Accessing a Hidden HTML field in the OnInit() method

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>




<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
alun65 [ Mi, 30 Januar 2008 07:45 ] [ ID #1919868 ]

Re: Accessing a Hidden HTML field in the OnInit() method

<alun65 [at] gmail.com> wrote in message
news:bb4ec0ba-bd50-4d32-8df2-84be83a60de0 [at] d21g2000prf.google groups.com...

> 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...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
mark [ Mi, 30 Januar 2008 08:27 ] [ ID #1919870 ]

Re: Accessing a Hidden HTML field in the OnInit() method

On Jan 30, 2:27=A0am, "Mark Rae [MVP]" <m... [at] markNOSPAMrae.net> wrote:
> <alu... [at] gmail.com> wrote in message
>
> news:bb4ec0ba-bd50-4d32-8df2-84be83a60de0 [at] d21g2000prf.google groups.com...
>
> > Sorry if I'm being dense and staring the obvious in the face. =A0As
> > always many thanks for any replies.
>
> When a page is posted back, page initialisation happens before ViewState h=
as
> been reloaded...
>
> --
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

In other words, (On)Init happens before Page_Load, right?
gnewsgroup [ Mi, 30 Januar 2008 16:31 ] [ ID #1919919 ]

Re: Accessing a Hidden HTML field in the OnInit() method

"gnewsgroup" <gnewsgroup [at] gmail.com> wrote in message
news:1c31fa04-3341-4282-b34d-b7c210ba2006 [at] d4g2000prg.googleg roups.com...

>>> 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...
>
> In other words, (On)Init happens before Page_Load, right?

Yes.


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
mark [ Mi, 30 Januar 2008 17:24 ] [ ID #1919935 ]
Microsoft » microsoft.public.dotnet.framework.aspnet » Accessing a Hidden HTML field in the OnInit() method

Vorheriges Thema: Disabling validation in an user control.
Nächstes Thema: ASP.NET and IIS Security