在 javascript 中访问 ASP HiddenField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8721313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:21:31  来源:igfitidea点击:

Accessing ASP HiddenField in javascript

c#javascriptasp.net

提问by Jordan

I have been searching through here and google for a few days now, trying to figure out why I cannot get the value of a hiddenfield variable in javascript. When accessed, the value is returned as undefined.
I have an ASP HiddenField inside an UpdatePanel which is part of a custom user control in a .aspx web page (standard issue).

我已经在这里和谷歌搜索了几天,试图找出为什么我无法在 javascript 中获取 hiddenfield 变量的值。访问时,该值返回为未定义。
我在 UpdatePanel 中有一个 ASP HiddenField,它是 .aspx 网页中自定义用户控件的一部分(标准问题)。

In my user control, I need to get the .Value of the HiddenField (hdnServer) in javascript after setting it in C#. But for some reason the following is not getting the correct value.

在我的用户控件中,我需要在 C# 中设置后在 javascript 中获取 HiddenField (hdnServer) 的 .Value。但由于某种原因,以下没有得到正确的值。

The MessageBox in the C# code returns the correct value (the code here has test values), but when accessed in javascript is undefined.

C# 代码中的 MessageBox 返回正确的值(这里的代码有测试值),但在 javascript 中访问时未定义。

userControl.ascx:

userControl.ascx:

//this function is called when the timer created in document.ready() elapses
//returns the correct hdnServer value in the check. 
    var checkHdn = function () {
        var temp = document.getElementById("<%=hdnServer.ClientID%>").value;
        temp = temp.toString();
        if (temp != "") {
            $('#LoadingViewer').hide();
            clearInterval(checkSrv);

            //enable start button
            $('#startBtn').attr("Enabled", "true");
        }
    };

  function RdpConnect() {

                //serverName = undefined here.  should be ip address when set in c# 
                var serverName = document.getElementById("<%= hdnServer.ClientID %>").value;
                alert(serverName);
                if (serverName != "") {
                    MsRdpClient.Server = serverName;
                }
            };

userControl.ascx.cs code-behind:

userControl.ascx.cs 代码隐藏:

public partial class userControl : System.Web.UI.UserControl
    {
        System.Timers.Timer timer; 

        protected void Page_Load(object sender, EventArgs e)
        {
            timer = new System.Timers.Timer(5000);
            timer.Start();
        }

        protected void testOnTick(object sender, System.Timers.ElapsedEventArgs e)
        {
                hdnServer.Value = "test value";
                startBtn.Enabled = true;
                timer.Enabled = false;
        }
    }

Here is the asp for HiddenField just in case: userControl.ascx:

这是 HiddenField 的 asp 以防万一:userControl.ascx:

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
    <Triggers>
         <!--trigger not used  -->
       <!-- <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />-->
    </Triggers>
    <ContentTemplate>
        <asp:HiddenField ID="hdnServer" runat="server" />
        <asp:Label ID="Label1" Text="Loading, please wait." CssClass="loading" runat="server"
            Font-Size="XX-Large" />
    </ContentTemplate>
</asp:UpdatePanel>

Thank you for any advice in advance!

提前感谢您的任何建议!

EDIT: messagebox removed.. Here is rendered html: http://pastie.org/3122247

编辑:消息框已删除..这是呈现的 html:http: //pastie.org/3122247

回答by scottm

You need to set ClientIDMode if you want to make it simple:

如果你想让它变得简单,你需要设置 ClientIDMode:

<asp:HiddenField runat="server" ClientIDMode="Static" Id="hidServer"/>

<script type="text/javascript">
  alert($("#hidServer").val());
</script>

Or, use the ClientID property if you don't set ClientIDMode:

或者,如果您未设置 ClientIDMode,请使用 ClientID 属性:

<asp:HiddenField runat="server" Id="hidServer"/>

<script type="text/javascript">
  alert($("<%= hidServer.ClientID %>").val());
</script>

回答by justinlabenne

User controls have always been a strange issue for referencing using js and then master pages to go along with it.

用户控件对于使用 js 和母版页进行引用一直是一个奇怪的问题。

For the hidden field do this:

对于隐藏字段,请执行以下操作:

<asp:HiddenField ID="hdnServer" runat="server" ClientIDMode="Static" />

in the js, do this:

在 js 中,执行以下操作:

var serverName = document.getElementById('hdnServer').value;

回答by Bin Taleb

you need to use 'not "like:

你需要使用'"喜欢:

var serverName = document.getElementById('<%= hdnServer.ClientID %>').value;

be careful don't use ". You have only use '

小心不要使用". 你只有使用'

回答by Steve Wellens

Try this:

试试这个:

var temp = $('#mytestcontrol_hdnServer').val();

回答by Robert Wilson

You can do this:

你可以这样做:

var temp = $('#hdnServer').val();

Instead of :

代替 :

var temp = document.getElementById("<%=hdnServer.ClientID%>").value;

Also change this:

也改变这个:

var serverName = document.getElementById("<%= hdnServer.ClientID %>").value;

To this:

对此:

var serverName = $('#hdnServer').val();