Asp.net 如何防止每次回发请求时调用函数

Asp.net 如何防止每次回发请求时调用函数,asp.net,c#-4.0,Asp.net,C# 4.0,我现在在回发上绑定了完整的菜单,每个回发请求函数 再次调用并绑定菜单我只想第一次调用,请建议 下面是我的代码 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 pleas

我现在在回发上绑定了完整的菜单,每个回发请求函数 再次调用并绑定菜单我只想第一次调用,请建议 下面是我的代码

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="";
}
}

您可以创建一个会话变量,然后检查该变量以确保代码只执行一次

您可以创建如下会话变量:

Session["myVar"] = "myText";
然后您可以检查它的值,如下所示:

((string)Session["myVar"]) == "myText"
使用 如果(!IsPostBack) {

--------------------------); --------------------------)

}

此条件下的所有函数或代码仅在请求页面时第一次运行。它不会在重新加载时执行

如果只想运行一次代码;当用户请求页面时,您可以使用上面建议的会话


如果只想在应用程序运行时第一次运行代码,则可以使用应用程序状态来控制代码

设置隐藏变量的位置hiddenMenuFlag?这不是MVC。请更正您的标记。@user3779305-以及如何将
bindMenuFlag
映射为
hiddenMenuFlag
?其类型错误隐藏字段名为hiddenMenuFlag