JQuery的母版与ASP.NET中 [英] JQuery with MasterPage in ASP.NET

查看:117
本文介绍了JQuery的母版与ASP.NET中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jQuery一些asp.net页面,使用母版页,而我在与加载的JQuery javascript文件的问题。

I'm trying to use JQuery with some asp.net pages, that use master pages, and I'm having problems with loading the JQuery javascript file.

当我包含在母版页的标记文件,它只是罚款上在同一个目录中的母版页页:

When I include the file in the markup of the master page, it works just fine on pages that are in the same directory as the master page:

<script src="jquery/jquery-1.4.2.min.js" type="text/javascript"></script>

但打破了不在同一个目录中的母版页页。母版页继承,其中包括他们的页面的路径,所以对jQuery的js文件的相对路径是不同的,不同的页面。而且,当然,我不能有主文件在不同的相对路径,因为只有其中的一个。

but that breaks for pages that aren't in the same directory as the master page. Master pages inherit the path of the page that includes them, so the relative path to the jquery .js file is different, for different pages. And, of course, I can't have different relative paths in the master file, because there's only one of them.

我可以使用绝对路径:

<script src="/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>

但如果网站被安装为一个虚拟目录,打破了。

but that breaks if the website is installed as a virtual directory.

我的下一个尝试是用〜表示该网站的根目录:

My next try was to use the "~" to indicate the root of the website:

<script src="~/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>

但脚本标签不明白了〜。

But the script tag doesn't understand the ~".

于是,我就做了code-后面。从的OnInit(),我尝试:

So I tried to do it in the code-behind. From OnInit(), I tried:

string url = "~/jquery/jquery-1.4.2.min.js";
url = this.ResolveUrl(url);
Page.ClientScript.RegisterClientScriptInclude("jquery_js", url);

和,让我错误的JQuery的JavaScript运行时。我在页面的标记一些javascript:

And that gives me errors when the JQuery javascript runs. I have some javascript in the markup of the page:

$(document).ready(function()
{
    ...
}

和我得到的$是不确定的。我添加警报开始的jQuery-1.4.2.min.js,它的装载OK,但在.aspx文件中此位的JavaScript后执行了。

and I get "$" is undefined. I added an alert to the beginning jquery-1.4.2.min.js, and it's loading OK, but after this bit of javascript in the .aspx file has executed.

我试图ScriptManager.RegisterClientScriptInclude(),相反,具有相同的结果。

I tried ScriptManager.RegisterClientScriptInclude(), instead, with the same result.

推荐答案

问题的核心是链接到的JavaScript文件被注入到页面 RegisterClientScriptInclude 正在下面放置在依靠它的标记中的JavaScript code中的页面上。

The core of the problem was that links to JavaScript files that were being injected into the page with RegisterClientScriptInclude were being placed on the page below the JavaScript code in the markup that depended on it.

什么是在 $(文件)。就绪()可能无法执行页面加载后到,但 $()立即执行的,因为它是由浏览器读出。

What's in $(document).ready() may not execute until after the page is loaded, but $() is executed immediately, as it is read by the browser.

的解决方案是移动的JavaScript以后的文件中出现。这并不总是看起来那么简单,当你在处理母版页和众多用户的控制。

The solution is move the JavaScript to appear later in the file. Which isn't always as simple as it appears, when you're dealing with master pages and multitudinous user controls.

对于我来说,来处理它最简单的方法是从code-背后注入的JavaScript,使用的RegisterStartupScript 。这种注入脚本块在表格末尾添加。他们中的可执行语句将执行所有不同的JavaScript文件已插入之后。

For me, the easiest way to handle it was to inject the JavaScript from the code-behind, using RegisterStartupScript. Script blocks injected with this are added at the end of the form. Executable statements in them will execute only after all the various JavaScript files have been inserted.

这篇关于JQuery的母版与ASP.NET中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆