首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

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

2012-03-29 
asp.net如何获取js变量的值js:script languagejavascript typetext/javascriptvar wscreen.width

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;

热点排行