Calling asp.net control from C# constructor

If you try to call an ASP.NET control from the constructor in C# page, you will get a null exception. This is because the control really did not get initialized at this point or the value was not yet assigned to it. So the constructor in ASP.NET page is really limited of what it can do.

To initialized variable from textboxes or other controls on page, use Page_load(Object Sender, EventArgs e) function

Page_load(Object Sender, EventArgs e)
{
   /* Initialize your variables here */
   name = txtStudentName.text; /* for example */
}
Advertisements
This entry was posted in Software Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s