Techie Talks

Friday, September 02, 2005

ASP.NET: Loading controls only once at runtime

Well, if the title isnt too great, the exact problem statement is this:
"I have an aspx page with some controls that need to be populated at runtime. However, they need not be loaded if the page is refreshed. How can this be achieved?"

Which, in other words, mean that certain section of code under the Page_Load() method of the page needs to be executed exactly once. It should not be executed when the page is refreshed (which also happens after a POST event, I guess).

Answer: Declare a static boolean variable inside the class, for example

static bool once = false;

set this flag inside the run-only-once section of Page_Load()

such as
... ...
if(!once)
{
//Run once section
once = true;
}


Side-Effect: Doing so, however, would result in the run-once not being exectued until the code is recompiled!! That is, if u run two instances of the same page, the run-once section will get executed only once!! I am still trying to resolve this!!

0 Comments:

Post a Comment

<< Home