Purvesh Desai
Purvesh Desai

Reputation: 1805

how do i get the value of HiddenField in page_init?

I have set HiddenField value from jQuery and i want to use this HiddenField value in page_init event, but each time i get blank value. what would be the issue.

$('#hfKitchenID').val(kitchenid);

protected void Page_Init(object sender, EventArgs e)
{
    string value = hfKitchenID.Value;
}

Upvotes: 0

Views: 2253

Answers (2)

Maitrey684
Maitrey684

Reputation: 782

I would like to suggest you that please use Request in Page Init event to get the value of hiddenfield control.

Please check the sample below ,hope it can help you.

1.Code in page(.aspx):

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
        <title></title>
        <script type="text/javascript" language="javascript">
            function SetValueToHidden() {
                var hidden = document.getElementById("HiddenField1");
                var text = document.getElementById("TextBox1");
                hidden.value = text.value;
            }
        </script> </head> <body>
        <form id="form1" runat="server">
        <div>
            Input text:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="ShowText" OnClientClick="SetValueToHidden()" />
            <asp:HiddenField ID="HiddenField1" runat="server" />
        </div>
        </form> 
</body> 
</html>

2.Code in page(.cs):

protected void Page_Init(object sender, EventArgs e)
    {
        if (Request["HiddenField1"] != null)
        {
            Response.Write(Request["HiddenField1"].ToString());
        }
    }

Upvotes: 0

Ghyath Serhal
Ghyath Serhal

Reputation: 7632

You cannot get the value of the hidden field on Page_init because the value of the hidden field is save in ViewState, and ViewState is not accessible on Page_Init.

In the page life cycle the LoadViewState Event occurs just after the Init Event.

Upvotes: 1

Related Questions