Sunil Khankriyal
Sunil Khankriyal

Reputation: 112

How to prevent calling of function on every postback request

I have bind complete menu on postback now every post back request function call and bind menu again i want to call it only first time please suggest here below is my code

if (!Page.IsPostBack)
{
objCommon = new Common();
Common.UpdateLoginSession();
if (hiddenMenuFlag.Value == "S")//used hidden field but not working as is
//does not retain value on post back please suggest
{
BindMenu("0");//here is function for binding menu
hiddenMenuFlag.Value="";
}
}

Upvotes: 0

Views: 211

Answers (2)

Abbas Kapasi
Abbas Kapasi

Reputation: 57

use if (!IsPostBack) {

--------------------------; --------------------------;

}

All functions or code inside this condition will run only for the first time, when page is requested. It won't execute on reload.

If you want to run a code only once; when the user request the page then you can use some session as suggested above.

If you want to run a code only for the first time when application runs, then you can use Application state to control your code

Upvotes: 1

user2771704
user2771704

Reputation: 6202

You could create a session variable and then check that variable to ensure your code will execute only once.

You create session variable like this:

Session["myVar"] = "myText";

And then you could check it value like below:

((string)Session["myVar"]) == "myText"

Upvotes: 1

Related Questions