【问题标题】:How to dynamically load a text into a user control label in asp.net?如何将文本动态加载到asp.net中的用户控件标签中?
【发布时间】:2018-06-01 10:03:51
【问题描述】:

我想加载 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);

【问题讨论】:

    标签: c# asp.net user-controls


    【解决方案1】:

    因为您的转换不正确,您应该转换为您的用户控件类型:

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

    【讨论】:

    • 你打败了我 4 秒。 ;)
    • :D :D :D 我想他不会相信这么快的回答
    • 我不知道我们必须对它进行类型转换...虽然非常简单的解决方案...你们以闪电般的速度回答了 XD。注意:我需要添加一个“using.ProjectName.UserControl”
    【解决方案2】:

    你必须输入 cast:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多