C# 在 ASP.NET 中向页眉添加控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/890376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 08:36:51  来源:igfitidea点击:

Add a control to the page header in ASP.NET

c#asp.netcustom-controls

提问by Lightweight

I'm creating a custom script control in ASP.NET

我正在 ASP.NET 中创建自定义脚本控件

The purpose of the control is simply a server variant of the tag, used to load javascript files The main purpose of this control however is to combine multiple scripts into one response so on the client side they see something like tag for each location, so all scripts registered in the DocumentTop location will be combined into a single tag with the exception of the location "inline", all inline scripts are rendered individually where they exist in the markup I have also created an httphandler, js.ashx, that does the actual combining of the scripts

该控件的目的只是标签的服务器变体,用于加载 javascript 文件 然而,该控件的主要目的是将多个脚本组合成一个响应,因此在客户端他们会看到每个位置的标签之类的东西,所以所有在 DocumentTop 位置注册的脚本将被合并到一个标签中,除了位置“内联”,所有内联脚本都单独呈现在它们存在于标记中的地方我还创建了一个 httphandler,js.ashx,它执行实际操作脚本的组合

Everything is working fine except for the "Head" location, for the two document locations i simply use the ClientScriptManager during prerender but for the Head location i have tried the following code during pre render

除了“头部”位置外,一切正常,对于两个文档位置,我只是在预渲染期间使用 ClientScriptManager 但对于头部位置,我在预渲染期间尝试了以下代码

var scriptControl = new HtmlGenericControl("script");
scriptControl.Attributes["language"] = "javascript";
scriptControl.Attributes["type"] = "text/javascript";
scriptControl.Attributes["src"] = src;
Page.Header.Controls.Add(scriptControl);

and I get the following error: The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.

并且我收到以下错误:在 DataBind、Init、Load、PreRender 或 Unload 阶段无法修改控件集合。

does anyone know how add a control to the page header from within a custom control?

有谁知道如何从自定义控件中向页眉添加控件?

Incidentally, the control is used on a content page that has two nested masters and also has a ScriptManager registered on the root master. The project is an asp.net 3.5 web application project

顺便提一下,该控件用于具有两个嵌套母版并且在根母版上注册了 ScriptManager 的内容页面。该项目是一个asp.net 3.5 web应用项目

采纳答案by Lightweight

Ive discovered an answer to my question.

我发现了我的问题的答案。

I don't quite understand the why but the problem lies in when I am trying to add my script control into the head, doing it in the control's PreRender event causes my error but if you add the control during the Page's PreRender event it all works fine and dandy eg:

我不太明白为什么但问题在于当我尝试将我的脚本控件添加到头部时,在控件的 PreRender 事件中执行此操作会导致我的错误,但是如果您在 Page 的 PreRender 事件期间添加控件,则一切正常好花花公子,例如:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    this.Page.PreRender += new EventHandler(Page_PreRender);
}

void Page_PreRender(object sender, EventArgs e)
{
    var scriptControl = new HtmlGenericControl("script");
    Page.Header.Controls.Add(scriptControl);
    scriptControl.Attributes["language"] = "javascript";
    scriptControl.Attributes["type"] = "text/javascript";
    scriptControl.Attributes["src"] = "blah.js";
}

回答by Canavar

I don't know why you get this error but how about using ClientScriptlike that :

我不知道您为什么会收到此错误,但是如何使用ClientScript

protected void Page_Load(object sender, EventArgs e)
{
    string scriptFile = "myscriptFile.js";

    if (!this.Page.ClientScript.IsClientScriptIncludeRegistered("myScript"))
    {
        this.Page.ClientScript.RegisterClientScriptInclude("myScript", scriptFile);
    }
}

ClientScriptManager.RegisterClientScriptInclude Method

ClientScriptManager.RegisterClientScriptInclude 方法

回答by ?ükrü Sa?lam

  protected void Page_Load(object sender, EventArgs e)
    {        
        HtmlGenericControl js = new HtmlGenericControl("script");
        js.Attributes["type"] = "text/javascript";
        js.Attributes["src"] = yol.ScriptYol("jquery-1.3.2.min.js");
        this.Page.Header.Controls.Add(js);

    }

回答by Cameron

protected override void OnInit(EventArgs e)
{
    EnsureChildControls();
    base.OnInit(e);
}