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

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

我想加载 2 个用户控件(相同但文本不同 属性)。 我的用户控件包含一个标签,该标签带有为 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);

您必须输入 cast:

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