How can I save asp:HiddenField value across postback?

前端 未结 8 1654
Happy的楠姐
Happy的楠姐 2020-12-06 12:36

How can I save asp:HiddenField value across postback?

相关标签:
8条回答
  • 2020-12-06 12:43

    This has nothing to do with ViewState. A form control's value is maintained by doing a POST. As long as the control is created early enough in the page lifecycle, the posted value will be set on the control. If you refresh the page or click on a hyperlink that does a GET, then the value will be lost or revert to the designer-generated default.

    Back to your question, if you have a designer-generated HiddenField (in the aspx file), it should automatically set the value on postback. Either you're changing it somewhere else in your code or you are trying to access the value before it has been set (i.e. before Page_Load()). If you have a code-generated HiddenField, it needs to have the same ID and be created before the Page sets the posted values, such as in OnInit.

    I would recommend you read through and understand the following articles. Otherwise, you will keep hitting walls because the Page lifecycle and ViewState are fundamental.

    http://msdn.microsoft.com/en-us/library/ms972976.aspx

    http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx

    0 讨论(0)
  • 2020-12-06 12:44

    Yes, asp:HiddenField inside an asp:UpdatePanel works.

    0 讨论(0)
  • 2020-12-06 12:50

    By default, it was built to do that. There shouldn't be an issue there unless you've disabled viewstate for the control, a parent control, or the page.

    0 讨论(0)
  • 2020-12-06 12:52

    It happens due to the update panel please check your update panel. use a single update panel for the whole page.

    0 讨论(0)
  • 2020-12-06 12:58

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
             <asp:HiddenField ID="hdnFld" Value="xyz" runat="server"/>
        </ContentTemplate>
    </asp:UpdatePanel>
    

    If you change the hidden fields value using JQuery and after that the page get refreshed, the hidden fields value will be the new value. now access the same using JQuery.

    var currentTab = $('#hdnFld').val();
    
    0 讨论(0)
  • 2020-12-06 13:02

    Placing and asp:hiddenfield inside an asp:UpdatePanel works.

    0 讨论(0)
提交回复
热议问题