如何获得服务器端ASP.NET的客户端值? [英] How do I get client-side value to server-side ASP.NET?

查看:124
本文介绍了如何获得服务器端ASP.NET的客户端值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用HiddenField将变量从客户端传递到服务器端,如下例所示: asp.net - 将客户端javascript值传递给服务器端C# - Stack Overflow



到目前为止我尝试了什么:

I tried to pass a variable from the client side to server side using HiddenField like in this example: asp.net - pass client side javascript value to server side C# - Stack Overflow

What I have tried so far:

<!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type ="text/javascript"> 
         <script type ="text/javascript"> 
                function passValue() {
                    document.getElementById('<%=HiddenField1.ClientID%>').value = "some stuff";
                }
                 window.onload = passValue;

            </script>
    </head>
    <body>    
        <form id="form1" runat="server">
        <div>    
            <asp:HiddenField ID="HiddenField1" runat="server"/>

            <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick ="passValue()"/>

        </div>
        </form>
    </body>
</html>





在服务器端,我有以下代码:



On the server side I have the following code:

protected void Page_Load(object sender, EventArgs e)
        {
            string a, b;
            if (IsPostBack)
            {
                Response.Write("The input value is: " + HiddenField1.Value.ToString());
                a = HiddenField1.Value.ToString();
            }
            else
            {
                Response.Write("The input value is: " + HiddenField1.Value.ToString());
                b = HiddenField1.Value.ToString();
            }
        }





当我的页面打开时,我希望在其上看到消息:输入值是:有些东西。仅当我按下按钮时才会显示该消息,因为仅在这种情况下IsPostBack变为true。我该怎么做才能在不按下按钮的情况下收到消息?提前致谢!



我的尝试:



上面的部分可以看到我到目前为止所尝试的内容。



When my page opens I expect to see on it the message: "The input value is: some stuff". The message appears only if I press the button because only in this case the IsPostBack becomes true. What should I do so get the message without pressing the button? Thanks in advance!

What I have tried:

In the above section can be seen what I have tried so far.

推荐答案

要获得预期的消息,那么你必须编写这样的代码

To get the message as you expected, then you will have to write the code like this
<asp:HiddenField ID="HiddenField1" Value="some stuff" runat="server" />





由于您要将值分配给客户端中的隐藏字段,因此只能在 PostBack 上访问该值



如果您将值分配给标记中的隐藏字段,那么您可以在Page上获取值加载(不回发)



如下更改值以查看其工作原理



Since you are assigning the value to the hidden field in Client Side, the value can be accessed only on the PostBack

if you are assigning the value to hidden field in the mark up then you can get the value on Page Load ( not postback )

change the value as below to see how it works

document.getElementById('<%=HiddenField1.ClientID%>').value = "some other more stuff";





现在,当页面第一次加载时,您将看到输出为输入值为:某些东西 将是输入值是其他更多东西


这篇关于如何获得服务器端ASP.NET的客户端值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆