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

灵雨飘零 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>
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------

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


如何解决这个问题呢??

...全文
242 19 打赏 收藏 转发到动态 举报
写回复
用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肯定会出错的。。。
此文档是从实际项目总结出来的,互联网上只有“在单一面进行语言切换”的教程, 没有“一键切换”的解决方案,更没有同时把5种读取资源文件总结在一起的demo。 本文档重点 1.多种读取方式 2.一键全站切换 步骤 一:新建一个asp.net 空 web应用程序“Internationalization”(图1) 二:右键“Internationalization”--->添加--->添加ASP.NET文件夹--->App_GlobalResources 三:右键“App_GlobalResources”--->添加--->新建项--->资源文件--->GlobalLanguage.resx 四:在GlobalLanguage.resx里添加键值对.(图二) 五:连续复制“GlobalLanguage.resx”两次到“App_GlobalResources”下面,并修改名称为 GlobalLanguage.en-US.resx GlobalLanguage.zh-CN.resx 说明:(为什么会有三个文件呢?系统必须有一个默认文件“GlobalLanguage.resx”,两种语言两个文件.) 六:修改英文和文资源文件里的键值对。 说明(网上大多做法都是把语言设置为浏览器自动识别语言, 这种方式在实际项目一般不用,所以直接讲解用程序来控制) 七:添加language1.aspx (图3,4) 八:添加language2.aspx (图5),在设计模式下“工具”--->“生成本地资源”,会自动添加一个“App_LocalResources” 和第五步一样,添加再添加两个资源文件。 language2.aspx.zh-CN.resx language2.aspx.en-US.resx 说明(一键全站切换的实现方法有多种,这里只讲最有效的,编写一个基类,在里面设置一个全局变量lan,所有 面都继承这个基类,再通过程序修改变量lan,用Session最简单,但是用户过多时对内存的消耗很大, 还可以用数据库和配置文件,但是最好用Cookie,所以下面以Cookie为例子) 九:新建一个母板“language.Master”(图6,7) 十:添加language3.aspx (图8,9),必须继承Base类 十一:添加language4.aspx (图10),必须继承Base类

62,042

社区成员

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

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

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

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