简体   繁体   中英

Accessing ASP HiddenField in javascript

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).

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.

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

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:

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:

<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

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

<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:

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

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

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

For the hidden field do this:

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

in the js, do this:

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

you need to use ' not " like:

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

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

尝试这个:

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM