HiddenField losing value on postback
Hi Imar,
I've a hidden field control on my page where its value gets lost on a postback. Is this normal? What I've is this:
In my html page, I've something like this:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:HiddenField ID="Hidden1" runat="server" />
.
.
.
<asp:Button ID="btnCancel" Text="Cancel" runat="server" />
</asp:Content>
In my code behind, I've something like this:
protected void Page_Init(object sender, EventArgs e)
{
if (Hidden1.Value == null || Hidden1.Value == "")
{
.
.
.
Hidden1.Value = "0";
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
.
.
.
Hidden1.Value = "1";
}
}
The problem is when I click the Cancel button, which invokes the Postback, thus the Page_Init event gets called but by that time the content of Hidden1 has been reset to empty/null. And there is no code in my html page or code behind where I clear the value of Hidden1. Am I missing something, or is this the normal behavior? I wound up having to use a session variable (which I don't really like using) instead of Hidden1 for the if condition in Page_Init.
Thanks for your time and help.
Ed
|