Click here to Skip to main content
16,016,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Page_PreInit(object sender, EventArgs e)
        {
//If i have a textbox with id t1, then eventhogh it is not initialized in this event(Untill Init event) then how am able to accessing the properties here
string text=t1.Text;

        }
 protected void Page_Init(object sender, EventArgs e)
        {
string text=t1.Text; //It should possible
        }
Posted

I would imagine that the said text box was "dragged-n-dropped" onto the page at design time and not created at runtime?? if thats the case, then its perfectly normal to be able to access that control anywhere on the page using the global reference. You will only get blank text in the PreInit event if the text property hasn't been set. If you look at the xxx.aspx.designer.cs file, you would see that when you create a new control on the web page, its global reference is added in the designer file using which you can access the control anywhere in the code behind file.

If you had declared the control in the Page_Init event then t1 wouldn't be accessible from Page_PreInit because it would have been out of context or scope.

Hope this helps.

Cheers...
 
Share this answer
 
at the stage of PreInit, control value will always be null. if you would like to use some sort of check or count...use global veriable instead.
PreInit is to set the page level properties before initializing its controls. like theme or title of the page.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900