asp.net動態載入ascx使用者控制項

來源:互聯網
上載者:User

標籤:des   style   blog   code   http   ext   

原文: asp.net動態載入ascx使用者控制項

在主aspx/ascx檔案中,將目標ascx1,ascx2控制項拖拉到其頁面中,然後刪除,目的是要產生:Register 代碼,然後在主檔案中定義DIV或Td之類的,設定ID ,runat="server",接下來LoadControl("ascx1")

 

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="loginState.ascx.cs" Inherits="v3_loginState" %>
<%@ Register Src="loginState1.ascx" TagName="loginState1" TagPrefix="uc1" %>
<%@ Register Src="loginState2.ascx" TagName="loginState2" TagPrefix="uc2" %>
<%@ Register Src="loginState3.ascx" TagName="loginState3" TagPrefix="uc3" %>
<link href="../v3/css.css" rel="stylesheet" type="text/css" />
<div id="logState" runat="server">

</div>
----------------

//是否登入
if (Session["VIPUSERNAME"] == null)
{
//沒有登入
ct = LoadControl("loginState1.ascx");
}
else
{}

logState.Controls.Clear();
logState.Controls.Add(ct);
----------------------

網上其它文章:

~動態加ascx~
// .aspx
例如:定義一個 <div id= "test " runat= "server " >
// .aspx.cs
Control uc = Page.LoadControl( "你的.ascx路徑 ");
test.Controls.Add(uc);
這時就可以顯示控制項uc的內容.其中要調用的控制項要註冊到本頁面
《詳細》
private CreateContorlUI SingleUIControl;//聲明使用者控制項

private void ControlAdd()
{
SingleUIControl = (CreateContorlUI)Page.LoadControl( "CreateSingleUI.ascx ");//載入使用者控制項,這裡使用者控制項和頁面在同一個目錄
SingleUIControl.ID = "SingelUI ";//設定使用者控制項ID

this.PlaceHolderSingle.Controls.Add(SingleUIControl);//添加使用者控制項到頁面
}

--------------

步驟一: 把ascx控制項拖入設計視窗,然後轉入html模式,保留定義
如果你需要多個ascx的話,請全部拖入。
但是把相應的內容刪除。(因為我們需要動態載入)
在需要填入控制項的地方,放個容器,比如td,並設定在伺服器端運行
如
<TD id="tdpan" runat=server></TD>

2:動態調用
UserControl myusercontrol = (UserControl) LoadControl ("../includes/pageNavigater.ascx") ;
Type myusertype = myusercontrol.GetType();
//下面是給ascx賦值
PropertyInfo myuserinfo1 = myusertype.GetProperty("RelatedDatagrid");//) .GetProperty("RelatedDatagrid");
myuserinfo1.SetValue(myusercontrol,gridhwcy ,null);


PropertyInfo mypassinfo = myusertype.GetProperty("torefresh");
mypassinfo.SetValue(myusercontrol,true,null );

PropertyInfo myuserdatasource = myusertype.GetProperty("RelatedDataSource");
myuserdatasource.SetValue(myusercontrol,dv,null);

tdpan.Controls.Clear();
tdpan.Controls.Add(myusercontrol);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.