Question

I need some help figuring out the best way to go about adding jquery code to my aspx file. We have a big front end solution in VS2008 using a master page. In the master page we have a ContentPlaceHolder where you can add stuff to the <head>, but the location of the place holder is above the code which declares the local path to jquery. Would I need to move the ContentPlaceHolder to a location below the jquery? If so, couldn't doing that cause some potential issues? I tried adding an additional ContentPlaceHolder under the script declaration tags, but that caused ViewState errors (I think having to do with code behind I added for my page to allow CSS3 for IE).

Here is the relevant code from the master file:

<%@ Master Language="C#" AutoEventWireup="True" CodeBehind="Class.Master.cs" Inherits="Company.Project.Masters.Class" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">

<title><%# Page.Title %></title>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<asp:ContentPlaceHolder ID="cphHead" runat="server" >
    </asp:ContentPlaceHolder>
<link href="~/inc/style.css" rel="stylesheet" type="text/css" />
<script src='<%# ResolveUrl("~/inc/utils.js") %>' type="text/javascript"></script>
<script type="text/javascript">
//some js stuff here
<style type="text/css">
        // some css stuff here
</style>

<script language="javascript" type="text/javascript" src='local/path/to/jquery' ></script>
<script language="javascript" type="text/javascript" src='local/path/to/jquery-ui-1.10.3.custom.min.js") %>'></script>

<script type="text/javascript">
    $(document).ready(function() {      
        //some jquery stuff here
        })
    }); 
</script>
</head>

Below is the code I have added to my page's .cs file to allow CSS3 elements to work in IE. This error looks like it is related to the ViewState error

protected void Page_Load(object sender, EventArgs e)
{     
       HtmlMeta xuac = new HtmlMeta();
        xuac.HttpEquiv = "X-UA-Compatible";
        xuac.Content = "IE=edge";
        Header.Controls.AddAt(0, xuac);
}

When I add a new ContentPlaceHolder in the MasterPage I get the error:

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

Pas de solution correcte

Autres conseils

Coming to all your points one by one.

You can have multiple content place holders in master page and you can refer to them in other pages. You should add a contentplaceholder right after jQuery script. And add content to this placeholder from child pages, it should not create any kind of problem.

Please share the view-state error you are getting on adding extra contentplaceholder. You can explore browser console for more information on view state problems.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top