简体   繁体   English

在asp.net回发后,隐藏变量没有值

[英]hidden variable does not have value after postback asp.net

I have following 我有以下

<input type="hidden" id="hdnField" name="hdnField"/>

Request.Form.Set("hdnField", x.ToString());

after the page post back the value is not there. 页面发回后,该值不存在。

I am new to this, any help would be appreciated. 我对此并不陌生,将不胜感激。

Source: 资源:

You could define a property in your page class and then modify the property value in your code: 您可以在页面类中定义属性,然后在代码中修改属性值:

    protected string HiddenFieldValue { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            HiddenFieldValue = x.ToString();
        else
            HiddenFieldValue = x.ToString();
    }

Then define the hidden form field like this so that it's value is set to the property value: 然后像这样定义隐藏的表单字段,以便将其值设置为属性值:

    <input type='hidden' id='hdnField' value='<%=HiddenFieldValue %>' />

If you only want to set the value form the property during a postback or non-postback you could add the condition as well: 如果只想在回发或不回发期间通过属性设置值,则也可以添加条件:

    <input type='hidden' id='hdnField' value='<% if(IsPostBack) { %> <%=HiddenFieldValue%> <% } %>' />

You should use asp:HiddenField tag provided by asp rather than using the basic HTML input. 您应该使用asp:HiddenField提供的asp:HiddenField标记,而不要使用基本的HTML输入。

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

Using this, you can read and write value in c# using hdnField.Value and in jQuery using $('#hdnField').val() . 使用此方法,您可以使用hdnField.Value在c#中读取和写入值,而在$('#hdnField').val()在jQuery中读取和写入值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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