用户控件以及JavaScript和母版页

时间:2022-10-19 21:31:20

Consider the following situation:

考虑以下情况:

There is a master page with a contentPlaceHolder for all javascript...

所有javascript都有一个带有contentPlaceHolder的母版页面...

for speed issuses this section is at the bottom of the page.

对于速度发布,此部分位于页面底部。

my user control generates some javascript that make use of some references in the master page javascript (Jquery library)

我的用户控件生成一些javascript,使用主页面中的一些引用javascript(Jquery库)

so, if my user control is rendered before the master page javascript, it won't work.

所以,如果我的用户控件在主页面javascript之前呈现,它将无法工作。

and this is my question:

这是我的问题:

how do I make some javascript code block inside my .ascx file to be rendered to the asp:Content JavaScript in the .aspx page

如何在我的.ascx文件中创建一些javascript代码块以呈现给.aspx页面中的asp:Content JavaScript

or maybe my all thinking is worng?

或者也许我的所有想法都是故事?

Thanks!

谢谢!

2 个解决方案

#1


3  

See if this works for you:

看看这是否适合你:

Where in the "bottom" of the Master Page? If you move it inside the closing server-side form control and use ClientScript.RegisterStartupScript in your user control, it will inject that script of your control "before the closing <form/> tag" (which will be after your your static script call).

在Master Page的“底部”哪里?如果在关闭服务器端表单控件中移动它并在用户控件中使用ClientScript.RegisterStartupScript,它将在“关闭

标记之前”(将在您的静态脚本调用之后)注入控件的脚本)。

Master Page - right above the closing server-side ASP.Net </form>:

母版页 - 位于关闭服务器端ASP.Net 的正上方:

....
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js "></script>
</form>

Content Page (nothing special):

内容页面(没什么特别的):

<%@ Register src= ....

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <uc1:jscript_inject ID="jscript_inject1" runat="server" />
</asp:Content>

User Control:

用户控制:

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "foo", @"alert('foo');", true);

}

This is how its rendered (HTML view source in browser):

这是它呈现的方式(浏览器中的HTML视图源):

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js "></script>

<script type="text/javascript">
    //<![CDATA[
    alert('foo');//]]>
</script>
....
</form>

As advertised, alert('foo'); is below the "pre-defined" jq cdn call....

正如所宣传的,警报('foo');低于“预定义”的jq cdn调用....

#2


0  

Cheers useful. Interestingly this works for Page.ClientScript.RegisterStartupScript but not Page.ClientScript.RegisterClientScriptInclude, and instead the 'include' gets added to the top of the form.

干杯很有用。有趣的是,这适用于Page.ClientScript.RegisterStartupScript,但不适用于Page.ClientScript.RegisterClientScriptInclude,而是将“include”添加到表单的顶部。

#1


3  

See if this works for you:

看看这是否适合你:

Where in the "bottom" of the Master Page? If you move it inside the closing server-side form control and use ClientScript.RegisterStartupScript in your user control, it will inject that script of your control "before the closing <form/> tag" (which will be after your your static script call).

在Master Page的“底部”哪里?如果在关闭服务器端表单控件中移动它并在用户控件中使用ClientScript.RegisterStartupScript,它将在“关闭

标记之前”(将在您的静态脚本调用之后)注入控件的脚本)。

Master Page - right above the closing server-side ASP.Net </form>:

母版页 - 位于关闭服务器端ASP.Net 的正上方:

....
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js "></script>
</form>

Content Page (nothing special):

内容页面(没什么特别的):

<%@ Register src= ....

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <uc1:jscript_inject ID="jscript_inject1" runat="server" />
</asp:Content>

User Control:

用户控制:

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "foo", @"alert('foo');", true);

}

This is how its rendered (HTML view source in browser):

这是它呈现的方式(浏览器中的HTML视图源):

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js "></script>

<script type="text/javascript">
    //<![CDATA[
    alert('foo');//]]>
</script>
....
</form>

As advertised, alert('foo'); is below the "pre-defined" jq cdn call....

正如所宣传的,警报('foo');低于“预定义”的jq cdn调用....

#2


0  

Cheers useful. Interestingly this works for Page.ClientScript.RegisterStartupScript but not Page.ClientScript.RegisterClientScriptInclude, and instead the 'include' gets added to the top of the form.

干杯很有用。有趣的是,这适用于Page.ClientScript.RegisterStartupScript,但不适用于Page.ClientScript.RegisterClientScriptInclude,而是将“include”添加到表单的顶部。