最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to include jquery in masterpage header without path problems? - Stack Overflow

programmeradmin7浏览0评论

I have tried this

<head id="Head1" runat="server">
<title>Back Office</title>
<link href="~/Styles/MasterPage.css" rel="stylesheet" type="text/css" />
<link href="Styles/custom-theme/jquery-ui-1.8.12.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/scripts/jquery-1.5.1.min.js") %>"></script>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>

The error message is

I think too much, it is just find using this sorry

I have tried this

<head id="Head1" runat="server">
<title>Back Office</title>
<link href="~/Styles/MasterPage.css" rel="stylesheet" type="text/css" />
<link href="Styles/custom-theme/jquery-ui-1.8.12.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/scripts/jquery-1.5.1.min.js") %>"></script>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>

The error message is

I think too much, it is just find using this sorry

Share Improve this question edited May 23, 2011 at 5:43 Sarawut Positwinyu asked May 23, 2011 at 5:22 Sarawut PositwinyuSarawut Positwinyu 5,04216 gold badges56 silver badges82 bronze badges 5
  • I have copy your line from your question and check, I am not getting an issue. – Muhammad Akhtar Commented May 23, 2011 at 5:35
  • 1 You don't need to resolve the URL on your resources... – Phill Commented May 23, 2011 at 5:39
  • Yes, i'm just found out that it is just fine using this <script type="text/javascript" src="/Scripts/jquery-1.5.1.min.js"></script> – Sarawut Positwinyu Commented May 23, 2011 at 5:43
  • @Muhammad i use visual studio 2010, asp 4. I don't know it might be some different. – Sarawut Positwinyu Commented May 23, 2011 at 5:49
  • yes, I have check in VS 2010 and asp 4 – Muhammad Akhtar Commented May 23, 2011 at 5:52
Add a ment  | 

2 Answers 2

Reset to default 3

It seems that, you are trying to add controls to page dynamically. In this case, you can use the below code

var control = new HtmlGenericControl("script") ;
control.Attributes.Add("type", "text/javascript");
control.Attributes.Add("src", Page.ResolveClientUrl("~/scripts/jquery-1.5.1.min.js"));
//CDN will be best while hosting the application
//control.Attributes.Add("src", "http://ajax.googleapis./ajax/libs/jquery/1.5.1/jquery.min.js");
this.Page.Header.Controls.Add(control);

Try using DataBinding with ResolveUrl

<script src="<%# ResolveUrl("~/Scripts/jquery-1.5.1.min.js") %>" 
          type="text/javascript"></script>

Then in your code behind call the Header's Databind

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Page.Header.DataBind();
    }

If you start getting errors regarding the ViewState you may need to disable it on the header.

<head runat="server" enableviewstate="false">
发布评论

评论列表(0)

  1. 暂无评论