web頁面動態載入UserControl,並調用使用者控制項中的方法來初始化控制項

來源:互聯網
上載者:User

標籤:style   blog   color   os   art   io   

1,HTML頁

頭部註冊:

<%@ Register Src="~/WorkLog/WorkLogNewV1/UserControl/CeShiBu.ascx" TagPrefix="UserControl" TagName="CeShiBu"%><%@ Register Src="~/WorkLog/WorkLogNewV1/UserControl/KaiFaBu.ascx" TagPrefix="UserControl" TagName="KaiFaBu"%>

控制項擺放位置:

 <div id="controlspan" runat=server>     </div>

2,C#後台

 /// <summary>    /// 根據事項和分類決定顯示使用者控制項中哪些要素    /// </summary>    /// <param name="strFL"></param>    /// <param name="strSX"></param>    private void setUserControlVisible(string strFL,string strSX)    {        if (ViewState["rzDeptName"].ToString() == "開發部")        {
//使用者控制項路徑 UserControl KaiFaBuControl
= (UserControl)Page.LoadControl("UserControl/KaiFaBu.ascx"); Type tc = KaiFaBuControl.GetType();
//使用者控制項中要調用的初始化方法 System.Reflection.MethodInfo m
= tc.GetMethod("OnStart");
//調用初始化方法要傳遞的參數
object[] bjParas = new object[1]; bjParas[0] = strSX; m.Invoke(KaiFaBuControl, bjParas); controlspan.Controls.Clear(); controlspan.Controls.Add(KaiFaBuControl); } if (ViewState["rzDeptName"].ToString() == "測試部") { UserControl KaiFaBuControl = (UserControl)Page.LoadControl("UserControl/CeShiBu.ascx"); Type tc = KaiFaBuControl.GetType(); System.Reflection.MethodInfo m = tc.GetMethod("OnStart"); object[] bjParas = new object[1]; bjParas[0] = strSX; m.Invoke(KaiFaBuControl, bjParas); controlspan.Controls.Clear(); controlspan.Controls.Add(KaiFaBuControl); } }

在頁面上動態改變strFL,strSX,可以動態載入不同的使用者控制項,或者載入使用者控制項中的不同元素。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.