Accessing a Hidden HTML field in the OnInit() method

A

alun65

I need to access a hidden HTML field on post back from the OnInit
method. This hidden field value has been set previously in the
Page_load event.

simplified example is as follows: IN THE CODE BEHIND

protected override void OnInit(EventArgs e)
{
Label1.Text = myHiddenFeild.Value;
}


protected void Page_Load(object sender, EventArgs e)
{
myHiddenFeild.Value = "Hello World";
}

protected void Button1_Click(object sender, EventArgs e)
{
//Just added so I could postBack
}


IN THE ASPX PAGE:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" runat="server" id="myHiddenFeild" />

<asp:Label ID="Label1" runat="server" Text="Label"></
asp:Label>
<br />
<br />
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>


Expected Result
---------------------
When I press the button the label is updated with the hidden field
value " Hello World".

Actual Result
-------------------
Is nothing happens.

Debugging
-----------------
When debugging the hidden field value (myHiddenFeild) is empty in the
Oninit method, but as soon as it the execution reaches the Page_load
method then the myHiddenFeild as a value again.

Sorry if I'm being dense and staring the obvious in the face. As
always many thanks for any replys.

Cheers
Alun
 
M

Mark Rae [MVP]

Sorry if I'm being dense and staring the obvious in the face. As
always many thanks for any replies.

When a page is posted back, page initialisation happens before ViewState has
been reloaded...
 
G

gnewsgroup

When a page is posted back, page initialisation happens before ViewState has
been reloaded...

In other words, (On)Init happens before Page_Load, right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top