Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi.. what is the use of page_load method and why we r write the code in this block and which type code write this block
Posted

page load the one of the event of your aspx page. We have to write code in this block to bind value the control like dropdown, gridview, dataview, repeater, datalist etc.
one more important thing to remember is that before binding you must have to check whether the page is postback or first time loaded by using "IsPostBack" property.
 
Share this answer
 
Comments
mahesh202 24-Nov-12 5:14am    
thanq and one doubt it is necessary to use "Ispostback" method..
Vishal.Shimpi144 24-Nov-12 5:28am    
Yes u have to always check the page is postback or fresh loaded, for that you have to use following code in page_load event
If(!IsPostBack)
{
// the code inside this block is only executed when your page is load for the first time

}

if you have not use this then your child controls(control on you page) will be bind every time and your control will not be postback.
The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.

Use the OnLoad event method to set properties in controls and to establish database connections.

[edit]unnecessary code block removed[/edit]
 
Share this answer
 
v2
When a client navigates to your web page, the first thing that happens is that the Page_Load event is fired at the server. This can do a huge variety of things, from checking authorization to loading relevant data based on who the user is (from his cookies) and what he was doing last time.

For example, if a user logs in and you store that in a cookie, you can check the cookie in page load and retrieve his name and ID, then use that to access your database and get his avatar and a list of all the items he has been thinking of buying. You can them build a page that is specific to the user, and which continues where he was last time.

It's a bit more complicated than this, but if you think of Page_Load happening as if each page was an application all on it's own and the event handler does the initialization for the application you won;'t be too far wrong.
 
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