简体   繁体   English

如何在asp.net中将文本动态加载到用户控件标签中?

[英]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). 我想加载2个用户控件(相同但具有不同的text属性)。 My user control consists of a label with a Function defined for text in ascx.cs 我的用户控件包含一个标签,该标签带有为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 我的.ascx文件

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

My .ascx.cs file 我的.ascx.cs文件

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

My .aspx file 我的.aspx文件

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

* i have registered the the control *我已经注册了控件

My .aspx.cs file 我的.aspx.cs文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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