【问题标题】:Unable to modify the control collection无法修改控件集合
【发布时间】:2019-08-24 00:47:14
【问题描述】:

我正在尝试从用户控件添加指向 Web 表单页面的链接。 CSS 文件包含控件使用的样式。当我尝试时,我得到以下异常:

在DataBind、Init、Load、 预渲染或卸载阶段

那么,什么时候可以将任何东西添加到控件集合中?在我看来,异常消息表明所有选项都不在讨论范围内。代码如下:

public partial class AddNoteDlg : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterFiles();
    }


    private void RegisterFiles()
    {
        Page.ClientScript.RegisterClientScriptInclude(GetType(), "addNoteDlgComponent-1.0.0.js", ResolveUrl("~/Shared/scripts/js/addNoteDlgComponent-1.0.0.js"));

        var css = new HtmlLink();
        css.Href = ResolveUrl("~/Shared/css/addNoteDlg-0.0.1.css");
        css.Attributes["rel"] = "stylesheet";
        css.Attributes["type"] = "text/css";
        css.Attributes["media"] = "all";

        Page.Controls.Add(css);
    }
}

【问题讨论】:

    标签: c# webforms


    【解决方案1】:

    相反,将链接标签卡在用户控件标记中,因为实际上没有区别。

    (以编程方式执行此操作的最初动机是我可以将其添加到标题中,使用Page.Header.Controls.Add(css),允许我将所有 CSS 链接保留在标题中。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      相关资源
      最近更新 更多