母板页中应用全局配置文件

灵雨飘零 2014-08-05 12:25:15
在ASP.NET程序中,如何在母板页中添加全局配置文件?
例如这样的话:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Index.master.cs" Inherits="Web.Template.Index" %>

<!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 runat="server">
<title> <%=Application["PageTitle"] %></title>
<meta name="keywords" content="<%=Application["PageKeywords"]%>" />
<meta name="description" content="<%=Application["Pagede"] %>" />

<link href="../CSS/Index/Index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>


会报错:

 </div>
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------

控件包含代码块(即 <% ... %>),因此无法修改控件集合。


如何解决这个问题呢??

...全文
267 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhll 2014-08-12
  • 打赏
  • 举报
回复
注意前面应该用单引号,具体看下面,还有报错需要加 ValidateRequest ,这样就不会报错并且能用了 比如:<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Index.master.cs" Inherits="Web.Template.Index" ValidateRequest="false" %> <!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 runat="server"> <strong><span style="color: #FF0000;"> <title> <%=Application["PageTitle"] %></title> <meta name="keywords" content='<%=Application["PageKeywords"]%>' /> <meta name="description" content='<%=Application["Pagede"] %>' /></span></strong> <link href="../CSS/Index/Index.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
sy401042879 2014-08-12
  • 打赏
  • 举报
回复

  <meta name="keywords" content="<%=Application["PageKeywords"]%>" />
    <meta name="description" content="<%=Application["Pagede"] %>" />
//单引号...
 <meta name="keywords" content=‘<%=Application["PageKeywords"]%>’ />
灵雨飘零 2014-08-12
  • 打赏
  • 举报
回复
引用 15 楼 sp1234 的回复:
[quote=引用 1 楼 kingboy2008 的回复:] 加了格式没转换竟然,应该是这样:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Index.master.cs" Inherits="Web.Template.Index" %>

<!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 runat="server">
    <title>
        <%=Application["PageTitle"] %></title>
    <meta name="keywords" content="<%=Application["PageKeywords"]%>" />
    <meta name="description" content="<%=Application["Pagede"] %>" />
    <link href="../CSS/Index/Index.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
 </div>
    </form>
</body>
</html>
这没有问题,可以运行。 另外,没有必要使用 Application 集合,那是 asp (10几年前)的程序写法。定义一个 public static 的变量用于绑定就行了。[/quote] 不可以的呢。
yzf86211861 2014-08-06
  • 打赏
  • 举报
回复
引用 15 楼 sp1234 的回复:
[quote=引用 1 楼 kingboy2008 的回复:] 加了格式没转换竟然,应该是这样:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Index.master.cs" Inherits="Web.Template.Index" %>

<!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 runat="server">
    <title>
        <%=Application["PageTitle"] %></title>
    <meta name="keywords" content="<%=Application["PageKeywords"]%>" />
    <meta name="description" content="<%=Application["Pagede"] %>" />
    <link href="../CSS/Index/Index.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
 </div>
    </form>
</body>
</html>
这没有问题,可以运行。 另外,没有必要使用 Application 集合,那是 asp (10几年前)的程序写法。定义一个 public static 的变量用于绑定就行了。[/quote] 大P神。
  • 打赏
  • 举报
回复
引用 1 楼 kingboy2008 的回复:
加了格式没转换竟然,应该是这样:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Index.master.cs" Inherits="Web.Template.Index" %>

<!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 runat="server">
    <title>
        <%=Application["PageTitle"] %></title>
    <meta name="keywords" content="<%=Application["PageKeywords"]%>" />
    <meta name="description" content="<%=Application["Pagede"] %>" />
    <link href="../CSS/Index/Index.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
 </div>
    </form>
</body>
</html>
这没有问题,可以运行。 另外,没有必要使用 Application 集合,那是 asp (10几年前)的程序写法。定义一个 public static 的变量用于绑定就行了。
jshi123 2014-08-06
  • 打赏
  • 举报
回复
把<%= .. %> 改成 <%# ... %> 并且在Page_Load方法中加上:Page.Header.DataBind(); 或者:在Head中再包裹一个PlaceHolder,把Head中的内容放到PlaceHolder中 http://stackoverflow.com/questions/778952/the-controls-collection-cannot-be-modified-because-the-control-contains-code-bl
灵雨飘零 2014-08-05
  • 打赏
  • 举报
回复
加了格式没转换竟然,应该是这样:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Index.master.cs" Inherits="Web.Template.Index" %>

<!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 runat="server">
    <title>
        <%=Application["PageTitle"] %></title>
    <meta name="keywords" content="<%=Application["PageKeywords"]%>" />
    <meta name="description" content="<%=Application["Pagede"] %>" />
    <link href="../CSS/Index/Index.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
 </div>
    </form>
</body>
</html>
  • 打赏
  • 举报
回复
js 操作session
  • 打赏
  • 举报
回复
js可以吗
灵雨飘零 2014-08-05
  • 打赏
  • 举报
回复
引用 9 楼 insus 的回复:
只能后台动态产生。
看来也只能后台生成了。其实我是想有没有前台可以解决的办法。
insus 2014-08-05
  • 打赏
  • 举报
回复
只能后台动态产生。
wangnaisheng 2014-08-05
  • 打赏
  • 举报
回复

/// 
/// 添加Meta标签 
/// 
/// 页面 
/// Meta名字 
/// Meta内容 
public void AddMeta(System.Web.UI.Page page, string name, string content) 
{ 
HtmlMeta meta = new HtmlMeta(); 
meta.Name = name; 
meta.Content = content; 
page.Header.Controls.Add(meta); 
} 

wangnaisheng 2014-08-05
  • 打赏
  • 举报
回复
引用 5 楼 kingboy2008 的回复:
[quote=引用 2 楼 wangnaisheng 的回复:] <%= %> 这种需要放在body 中,放在head肯定会出错的。。。
<title> <%=Application["PageTitle"] %></title> <meta name="keywords" content="<%=Application["PageKeywords"]%>" /> <meta name="description" content="<%=Application["Pagede"] %>" /> 这些信息不能放到Body中吧 [/quote] http://zjc369.blog.sohu.com/19374962.html 试试写后台呢
灵雨飘零 2014-08-05
  • 打赏
  • 举报
回复
灵雨飘零 2014-08-05
  • 打赏
  • 举报
回复
引用 12 楼 Hsuifengershi 的回复:
js 操作session
要用全局变量Application。
灵雨飘零 2014-08-05
  • 打赏
  • 举报
回复
引用 2 楼 wangnaisheng 的回复:
<%= %> 这种需要放在body 中,放在head肯定会出错的。。。
<title> <%=Application["PageTitle"] %></title> <meta name="keywords" content="<%=Application["PageKeywords"]%>" /> <meta name="description" content="<%=Application["Pagede"] %>" /> 这些信息不能放到Body中吧
myhope88 2014-08-05
  • 打赏
  • 举报
回复
楼上正解~~~
wangnaisheng 2014-08-05
  • 打赏
  • 举报
回复
<%= %> 这种需要放在body 中,放在head肯定会出错的。。。

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧