![](/img/trans.png)
[英]How to set public bool value in C# code behind, read in .ascx User Control?
[英]How to embed user control (ascx) from code behind C#?
將用戶控件嵌入html頁面很容易,但是如何從c#背后的代碼動態地嵌入用戶控件呢? 任何幫助/建議將不勝感激。
您可以使用LoadControl()方法並將其加載到內容控件中
例:
protected void Page_Load(object sender, EventArgs e)
{
controls_example exmp = LoadControl("controls/example.ascx") as controls_example;
myContentPanel.Controls.Add(exmp);
}
使用LoadControl
可以動態加載和創建控件。 然后,您可以將其添加到頁面的控件集合中。
UcType myControl = (UcType) LoadControl("UcType.ascx");
this.SomeContainerContainerControl.Controls.Add(myControl);
其中, UcType
是UserControl
的類型, SomeContainerContainerControl
是要添加此uc的控件,如Panel
。 如果要將其添加到頁面頂部,只需使用this.Controls.Add(myControl)
。
您應該將其添加到Page_Init
或Page_Load
(最晚)。 您必須像每個其他動態控件一樣在每個回發中重新創建它,並具有與以前相同的ID(如果有)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.