简体   繁体   中英

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

I want to load 2 user controls(same but with different text property). My user control consists of a label with a Function defined for text in ascx.cs

i am loading the control at run time using a panel ..here i want both the user control label to have different texts.

My .ascx file

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

My .ascx.cs file

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

My .aspx file

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

* i have registered the the control

My .aspx.cs file

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

because you are making incorrect casting, you should cast to your user control type :

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM