当前位置: 代码迷 >> ASP.NET >> asp.net怎么获取js变量的值
  详细解决方案

asp.net怎么获取js变量的值

热度:3797   发布时间:2013-02-25 00:00:00.0
asp.net如何获取js变量的值
js:
 <script language="javascript" type="text/javascript">
   
  var w=screen.width; 
  var h=screen.height; 
  alert( "当前显示器分辨率 "+w+ "* "+h); 

  </script>

在js 中定义了w和h两个变量,分别取得显示器分辨率的宽和长,想要把这两个变量的值传递到.aspx页面的page_load中,如何实现
 protected void Page_Load(object sender, EventArgs e)
  {
   
  TextBox1.Text = "height = " + Request.Params["h"].ToString();
   
  TextBox2.Text = "width = " + Request.Params["w"].ToString();
  }
这样做不行,提示 w 和h 是空值

------解决方案--------------------------------------------------------
你想在后台拿肯定,前台要送过去,要不提交表单,要不ajax请求,作为表单或参数提交到后台,才能这么写~
------解决方案--------------------------------------------------------
HTML code
    <script src="js/jquery-1.5.2.min.js" type="text/javascript"></script>    <script type="text/javascript">        $(function() {            $.post("CacheDemo.aspx", { "strWidth": window.screen.width,"strHeight":window.screen.height }, function(data, status) {                if (status == "success") {                }            });});    </script>
------解决方案--------------------------------------------------------
js:
 <script language="javascript" type="text/javascript">

var w=screen.width;
var h=screen.height;
//alert( "当前显示器分辨率 "+w+ "* "+h);
document.getElementById("w").value = w;
document.getElementById("h").value = h;

</script>
页面:
<asp:hiddenfield id="w" runat="server" value=""></asp:hiddenfield>
<asp:hiddenfield id="h" runat="server" value=""></asp:hiddenfield>

page_load中,
 protected void Page_Load(object sender, EventArgs e)
{

TextBox1.Text = "height = " + h.value;

TextBox2.Text = "width = " + w.value;

  相关解决方案