我想在一个地方访问所有.aspx页面的page_load
事件。
场景: -
我在WebForms(非MVC)中有一个asp.net网站。它有70-75 .aspx
页。
现在,我想在所有页面的page_load
事件中执行一些相同的业务逻辑。
我的问题: -
现在如何在一个地方访问所有75个page_load
个网页中的.aspx
,以便当时用户请求form1.aspx
或form2.aspx
或form58.aspx
我想称之为我的共同功能。
我尝试过的事情: -
为了实现上述方案,我使用了Application_BeginRequest
的Global.asax
文件事件,该事件将针对每个请求进行调用。 但现在的问题是,Application_BeginRequest
也会在button_click
上调用{strong} ajax call
,我不想要即可。我只想在page_load
事件被触发时调用我的常用函数。
是否有其他方法可以使用httpmodule
或httphandler
来实现上述方案 - 如果是,请帮助我。
谢谢,
物理上称每个page_load
中的每个常见函数都是坏主意。我希望它在共同的地方。
答案 0 :(得分:0)
尝试创建基类并使所有页面继承它并在所有页面加载中从base调用一些函数。
否则您可以覆盖基类中的页面生命周期事件。
并检查这个以检查请求是否在global.asax
中回发How do I detect if a request is a callback in the Global.asax?
答案 1 :(得分:0)
您可以这样做:
1. create a separate class file Common_Page_Load.cs
2. Write your logic inside that class file Common_Page_Load.cs
3. Call that class file in every page_load function:
form1.aspx or form2.aspx or form58.aspx.
4. If that needs to be conditional,
then create a Session variable and you might use it accordingly.
例如:
class Common_Page_Load { // separate .cs file
public bool some_logic() {
...
if(...){
return true;}
else{
return false;}
}
}
Page_Load(...) { //page load of form 1 or form 2 or form 50 or so
if(Session["check_logic"] == "true"){
Common_Page_Load CP = new CP();
bool myvar = CP.some_logic();
...
...
}
}
希望它有所帮助。