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

c# – 在javascript中访问ASP HiddenField

发布时间:2020-12-16 00:02:06 所属栏目:百科 来源:网络整理
导读:我一直在这里搜索和谷歌几天,试图弄清楚为什么我不能在 javascript中获得隐藏字段变量的值.访问时,该值将返回undefined. 我在UpdatePanel中有一个ASP HiddenField,它是.aspx网页中自定义用户控件的一部分(标准问题). 在我的用户控件中,我需要在C#中设置后在j
我一直在这里搜索和谷歌几天,试图弄清楚为什么我不能在 javascript中获得隐藏字段变量的值.访问时,该值将返回undefined.
我在UpdatePanel中有一个ASP HiddenField,它是.aspx网页中自定义用户控件的一部分(标准问题).

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

C#代码中的MessageBox返回正确的值(此处的代码具有测试值),但是在javascript中访问时未定义.

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代码隐藏:

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;
        }
    }

这里是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>

提前感谢您的任何建议!

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

解决方法

如果要使其简单,则需要设置ClientIDMode:

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

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

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

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

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

(编辑:李大同)

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

    推荐文章
      热点阅读