ASP.NET中,動態載入使用者控制元件
ASP.NET中,動態載入使用者控制元件,有些人可能會碰到使用者控制元件中的事件(比如按鈕等)沒有觸發,使用者控制元件消失等情形。我也曾遇到這樣的情況,將一些經驗總結如下,實際上,如果你對ASP.NET的頁面模型及其生命週期很熟悉的話,這樣的問題很容易想到解決方法的。
使用者控制元件中的事件會導致其所在的頁面回發,在回發時必須將使用者控制元件重新載入。
在載入使用者控制元件的方法中,將最後一次載入的使用者控制元件路徑儲存起來,以便在頁面Load方法中重新載入該控制元件。
protected void Page_Load( object sender , EventArgs e ){
if( this.LatestLoadedControlName != "" )
this.LoadUserControl( LatestLoadedControlName , container );
}
protected string LatestLoadedControlName
{
get
{
return (string)ViewState["LatestLoadedControlName"];
}
set
{
ViewState["LatestLoadedControlName"] = value;
}
}
public void LoadUserControl(string controlName, Control container)
{
//先移出已有的控制元件
if (LatestLoadedControlName != null)
{
Control previousControl = container.FindControl(LatestLoadedControlName.Split('.')[0]);
if (previousControl != null)
{
container.Controls.Remove(previousControl);
}
}
string userControlID = controlName.Split('.')[0];
Control targetControl = container.FindControl(userControlID);
if (targetControl == null)
{
UserControl userControl = (UserControl)this.LoadControl(controlName);
userControl.ID = userControlID;
container.Controls.Add(userControl);
LatestLoadedControlName = controlName;
}
}
使用者控制元件中的事件會導致其所在的頁面回發,在回發時必須將使用者控制元件重新載入。
在載入使用者控制元件的方法中,將最後一次載入的使用者控制元件路徑儲存起來,以便在頁面Load方法中重新載入該控制元件。
protected void Page_Load( object sender , EventArgs e ){
if( this.LatestLoadedControlName != "" )
this.LoadUserControl( LatestLoadedControlName , container );
}
protected string LatestLoadedControlName
{
get
{
return (string)ViewState["LatestLoadedControlName"];
}
set
{
ViewState["LatestLoadedControlName"] = value;
}
}
public void LoadUserControl(string controlName, Control container)
{
//先移出已有的控制元件
if (LatestLoadedControlName != null)
{
Control previousControl = container.FindControl(LatestLoadedControlName.Split('.')[0]);
if (previousControl != null)
{
container.Controls.Remove(previousControl);
}
}
string userControlID = controlName.Split('.')[0];
Control targetControl = container.FindControl(userControlID);
if (targetControl == null)
{
UserControl userControl = (UserControl)this.LoadControl(controlName);
userControl.ID = userControlID;
container.Controls.Add(userControl);
LatestLoadedControlName = controlName;
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-526850/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- DLL動態庫動態載入
- 動態載入UserControl
- python動態載入(三)Python
- vue 動態載入元件Vue元件
- Java動態載入類Java
- goloader - golang動態載入Golang
- QLibrary 載入動態庫
- ListView動態載入資料View
- 指令碼的動態載入指令碼
- 使用dlopen載入動態庫
- echarts遷移圖動態載入Echarts
- OrchardCore 如何動態載入模組?
- Qt動態新增控制元件QT控制元件
- Android native層動態載入so庫Android
- 動態載入的一些坑
- vue如何動態載入本地圖片Vue地圖
- javascript如何動態載入js檔案JavaScriptJS
- 動態計算控制元件高度控制元件
- 【轉載】WPF中TreeView控制元件資料繫結和後臺動態新增資料(一)View控制元件
- ASP.NET 動態資料支援ASP.NET
- 啟動優化之動態庫延遲載入優化
- 載入動態連結庫——dlopen dlsym dlclose
- vue後臺管理之動態載入路由Vue路由
- SyntaxHighlighter 頁面動態js載入方式整理JS
- Drools與動態載入規則檔案
- asp.net mvc中的使用者登入驗證過濾器ASP.NETMVC過濾器
- 動態綁資料(Repeater控制元件控制元件
- vue 動態選單以及動態路由載入、重新整理採的坑Vue路由
- jQuery 動態載入下拉框選項(Django)jQueryDjango
- Jquery Datatables (2) 動態載入資料型別jQuery資料型別
- ElementUI級聯選擇器動態載入DemoUI
- Unity3D動態載入FBX檔案Unity3D
- 優雅的實現動態載入 css、jsCSSJS
- Protobuf 動態載入 .proto 檔案並操作 Message
- JavaScript系列:動態建立iframe並載入頁面JavaScript
- React Native 中實現動態匯入React Native
- 藉助 Webpack 靜態分析能力實現程式碼動態載入Web
- [譯]React中的使用者認證(登入態管理)React
- Asp.Net Core入門之靜態檔案ASP.NET