Techie Talks

Friday, September 02, 2005

ASP.NET: Loading controls only once at runtime - Solved!

The side effect that I mentioned in the previous post has been solved!
Solution: In order to solve the problem under question, from the previous post, of executing a section of Page_Load only when a page is loaded for the first time, the Page.IsPostBack flag is used. This flag tells you whether the reason for the Page loading is to reflect some post back action. Thus, our run-once section can be written inside the Page_Load() method as:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        //Run-once section
    }
}

The use of static variable mentioned earlier for this purpose is, thus, NOT a good idea to solve this problem!

0 Comments:

Post a Comment

<< Home