[英]Access Page_Load event for All .aspx pages at one place
我想在一處訪問所有.aspx頁的page_load
事件。
場景: -
我在WebForms中有一個asp.net網站(不是MVC)。 它具有70-75 .aspx
頁。 現在,我想在所有頁面的page_load
事件中執行一些相同的業務邏輯。
我的問題:-
現在,我如何才能在一個地方訪問所有75個.aspx
頁面的page_load
,因此,每當用戶form1.aspx
請求form1.aspx
或form2.aspx
或form58.aspx
時,我都想調用我的公用函數。
我一直嘗試到:
為了實現上述場景,我使用了Global.asax
文件的Application_BeginRequest
事件,該事件將針對每個請求進行調用。 但現在的問題是,這個Application_BeginRequest
也可以在button_click
, 我不希望的 ajax call
上ajax call
。 我只想在觸發page_load
事件時調用我的常用函數。
還有其他方法可以使用httpmodule
或httphandler
實現上述方案-如果是,請幫助我。
謝謝,
在每個page_load
物理地調用該通用函數是一個壞主意。 我想要在普通地方。
嘗試創建一個基類,並使所有頁面繼承它,並在所有頁面加載中從base調用某些函數。
否則,您可以在基類中覆蓋頁面生命周期事件。
並檢查此內容以檢查請求是否在global.asax中回發
您可以通過以下方式進行操作:
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();
...
...
}
}
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.