0

我有一个 Web 表单,我在其中加载了一个 Web 用户控件(A)

当单击控件 A 上的按钮时,它会加载另一个名为的 Web 用户控件的数组infobox

当我将用户控件添加到 Web 用户控件 A 的控件集合中时,它不会显示。

下面是代码:点击控件A的按钮

foreach (Categories category in CategoriesDataList)
            {
                InfoBox ib = new InfoBox();
                ib.LiteralName = category.Category_Name;
                span_tempList.Controls.Add(ib);
                ib = null;           
            }   



span_tempList is a `<span>` tag with `runat=server`

下面是 InfoBox 控件的 HTML 和 cs 代码

 public partial class InfoBox : System.Web.UI.UserControl
    {
        protected global::System.Web.UI.WebControls.Label ltrlName = new System.Web.UI.WebControls.Label();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public InfoBox()
        {

        }

        public string LiteralName
        {
            get { return ltrlName.Text; }
            set { ltrlName.Text = value; }
        }

    }

--HTML--

<div style="width:10%;float:left;">
        <asp:Label ID="ltrlName" runat="server" Text="Label"></asp:Label>
    </div>
    <div style="width:20%;float:left;">
        <asp:Image ID="imgPicture" runat="server" ImageUrl="" />
    </div>
    <div style="width:70%;float:right;">

    </div>

还尝试向页面添加控件不起作用

4

1 回答 1

3

如果要将其添加到页面的控件集合中,则不应通过构造函数创建 UserControl 的实例,而应使用LoadControl 。

InfoBox ib = (InfoBox)LoadControl("InfoBox.ascx");

另外,您为什么将控件设置为null?它将在页面生命周期结束时自动处理。

于 2012-01-05T13:26:51.397 回答