加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

如何从Asp.Net传递JavaScript的价值

发布时间:2020-12-16 09:56:16 所属栏目:asp.Net 来源:网络整理
导读:如何在我的TextBox中写x和y,我写这个但不是Work script language="javascript" type="text/javaScript" function PantallaResolucion() { var x = screen.width.toString(); var y = screen.height.toString(); var xx = document.getElementById("HiddenFie
如何在我的TextBox中写x和y,我写这个但不是Work

<script language="javascript" type="text/javaScript">
     function PantallaResolucion()
     {
         var x = screen.width.toString();
         var y = screen.height.toString();
         var xx = document.getElementById("HiddenField1");
         var yy = document.getElementById("HiddenField2");
         xx.value = x;
         yy.value = y;
     }
</script>

我的asp.net代码

<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:HiddenField ID="HiddenField2" runat="server"  />

C#代码

protected void Page_Load(object sender,EventArgs e)
{
    string c = "<script language='javascript'> PantallaResolucion(); </script> ";
    ClientScript.RegisterStartupScript(this.GetType(),"PantallaResolucion();",c);
    TextBox1.Text = HiddenField1.Value.ToString() + "x" + HiddenField2.Value.ToString();// NOT WORK**
}

解决方法

您可以尝试以下方法:

var xx = document.getElementById("<%= HiddenField1.ClientID%>");
var yy = document.getElementById("<%= HiddenField2.ClientID%>");

您可以查看here以查看有关为何需要这样做的详细说明.根据这个链接,用几句话说:

When a Web server control is rendered as an HTML element,the id
attribute of the HTML element is set to the value of the ClientID
property. The ClientID value is often used to access the HTML element
in client script by using the document.getElementById method.

对整个问题采取更明确的方法如下:

将此脚本放在页面底部,在结束标记< / body>之前

<script>
    function PantallaResolucion(){
        var width = screen.width.toString();
        var height = screen.height.toString();
        var hiddenFld1 = document.getElementById("<%= HiddenField1.ClientID%>");
        var hiddenFld2 = document.getElementById("<%= HiddenField2.ClientID%>");
        hiddenFld1.value = width;
        hiddenFld2.value = height;
        var textBox1 = document.getElemenetById("<%=TextBox1.ClientID%>");  
        textBox1.value =  hiddenFld1.value + "x" + hiddenFld2.value;
    }
</script>

然后清除Page_Load方法中的语句.顺便说一下,我认为没有任何理由可以使用这两个隐藏的输入.您可以删除它们并对上述脚本进行相应的更改.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读