【发布时间】:2026-01-08 12:15:02
【问题描述】:
我需要一些帮助来找出将 jquery 代码添加到我的 aspx 文件的最佳方法。我们在 VS2008 中有一个使用母版页的大型前端解决方案。在母版页中,我们有一个 ContentPlaceHolder,您可以在其中向<head> 添加内容,但占位符的位置位于声明 jquery 本地路径的代码上方。我需要将 ContentPlaceHolder 移动到 jquery 下面的位置吗?如果是这样,这样做不会导致一些潜在的问题吗?我尝试在脚本声明标签下添加一个额外的 ContentPlaceHolder,但这会导致 ViewState 错误(我认为这与我为我的页面添加的代码有关,以允许 IE 使用 CSS3)。
以下是主文件中的相关代码:
<%@ 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>
下面是我添加到页面的 .cs 文件中的代码,以允许 CSS3 元素在 IE 中工作。这个错误看起来与 ViewState 错误有关
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);
}
当我在 MasterPage 中添加新的 ContentPlaceHolder 时出现错误:
加载视图状态失败。视图状态所在的控制树 正在加载的必须与用于保存的控制树匹配 上一个请求期间的视图状态。例如,当添加 动态控制,在回发期间添加的控件必须匹配 在初始阶段添加的控件的类型和位置 请求。
【问题讨论】:
标签: c# jquery asp.net css master-pages