簡體   English   中英

如何在asp.net中將文本動態加載到用戶控件標簽中?

[英]How to dynamically load a text into a user control label in asp.net?

我想加載2個用戶控件(相同但具有不同的text屬性)。 我的用戶控件包含一個標簽,該標簽帶有為ascx.cs中的文本定義的功能

我正在使用面板在運行時加載控件。在這里,我希望兩個用戶控件標簽都具有不同的文本。

我的.ascx文件

 <asp:Label ID="uclbl" runat="server" />

我的.ascx.cs文件

 public string textforlabel
    {
        get { return uclbl.Text; }
        set { uclbl.Text = value; }
    }

我的.aspx文件

  <asp:Panel ID="panelMain" runat="server" >
   </asp:Panel>

*我已經注冊了控件

我的.aspx.cs文件

Control _dummyUserControl = (Control)Page.LoadControl("~/UserControl/User1.ascx");
        _dummyUserControl.  ; //can not find the textforlabel here
        panelMain.Controls.Add(_dummyUserControl);

因為您進行了不正確的轉換,所以應該轉換為用戶控件類型:

User1 _dummyUserControl = (User1)Page.LoadControl("~/UserControl/User1.ascx");
 _dummyUserControl.MyProperty = "SDfsdf"  ; //here you can find all your custom properties
panelMain.Controls.Add(_dummyUserControl);

您必須輸入強制轉換:

User1 _dummyUserControl = (User1)Page.LoadControl("~/UserControl/User1.ascx");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM