如何定义一个变量,每个页面都可以访问:(

buyifly 2006-06-21 08:48:08
我想定义一个变量,siteName
变量值是从数据库中取得的网站名称
我想使每个页面的title是是<%=siteName%>
怎么定义才可以每个页面都使用呢??
...全文
237 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yesur 2006-06-21
  • 打赏
  • 举报
回复
部分代码
Web.Config
------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CMSConfig" type="SystemFramework.Config,SystemFramework"/>
</configSections>
<CMSConfig>
<add key="ConnectionString" value="Initial Catalog=EDZH.COM;server=(local);User ID=sa;Password="/>
<add key="CopyRight" value="网络大杂烩(edzh.com)"/>
</CMSConfig>
<system.web>

---------------------------------------
Config.cs
using System;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;
namespace SystemFramework
{
/// <summary>
/// CMSConfig 的摘要说明。
/// </summary>
public class Config : IConfigurationSectionHandler
{
#region 默认属性
private const string DEFAULT_ConnectionString = "";
private const string DEFAULT_CopyRight = "网络大杂烩(edzh.com)";
#endregion

#region 初始化设置方法
/// <summary>
/// 初始化设置,在GLOBAL.ASAX中运行
/// </summary>
/// <param name="WWWRootPath"></param>
public static void OnApplicationStart(string WWWRootPath)
{
System.Configuration.ConfigurationSettings.GetConfig("CMSConfig");
Config._wwwroot = WWWRootPath;
}
/// <summary>
/// 初始化设置,设置数据库连接,web路径
/// </summary>
public static void setConnection()
{
DataLayer.DataBase.ConnectionString = Config.ConnectionString;
DataLayer.DataBase.WWWROOT = Config.WWWRoot;
DataLayer.DataBase.Company = Config.CopyRight;
}
#endregion

#region 属 性
/// <summary>
/// 版权所有
/// </summary>
public static string CopyRight
{
get { return _copyright; }
}
private static string _copyright = DEFAULT_CopyRight;

/// <summary>
/// 数据库连接字符串
/// </summary>
/// <value>string</value>
public static string ConnectionString
{
get {return _connectionString;}
}
private static string _connectionString = DEFAULT_ConnectionString;

#endregion

#region 方法
/// <summary>
/// 取得webconfig数据
/// </summary>
/// <param name="parent"></param>
/// <param name="configContext"></param>
/// <param name="section"></param>
/// <returns></returns>
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
NameValueCollection settings;
try
{
NameValueSectionHandler baseHandler = new NameValueSectionHandler();
settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
}
catch
{
settings = null;
}

if (settings == null)
{
_connectionString = DEFAULT_ConnectionString;
_copyright = DEFAULT_CopyRight;
}
else
{
//连接数据库语句在这里就解密
_connectionString = ReadSetting(settings, "connectionString", DEFAULT_ConnectionString);
_copyright = ReadSetting(settings, "copyright", DEFAULT_CopyRight);
}
return null;
}


/// <summary>
/// 取得Config 中节点对象是string的值
/// </summary>
/// <param name="settings">节点对象</param>
/// <param name="key">节点名称</param>
/// <param name="defaultValue">缺省值</param>
/// <returns>string</returns>
public static String ReadSetting(NameValueCollection settings, String key, String defaultValue)
{
try
{
Object setting = settings[key];
return (setting == null) ? defaultValue : (String)setting;
}
catch
{
return defaultValue;
}
}

/// <summary>
/// 取得Config 中节点对象是int的值
/// </summary>
/// <param name="settings">节点对象</param>
/// <param name="key">节点名称</param>
/// <param name="defaultValue">缺省值</param>
/// <returns>int</returns>
public static int ReadSetting(NameValueCollection settings, String key, int defaultValue)
{
try
{
Object setting = settings[key];

return (setting == null) ? defaultValue : Convert.ToInt32((String)setting);
}
catch
{
return defaultValue;
}
}
#endregion
}
}


-------------------------------------------------------------
Global.aspx.cs

protected void Application_Start(Object sender, EventArgs e)
{
//初始化各种属性
SystemFramework.Config.OnApplicationStart(Context.Server.MapPath(Context.Request.ApplicationPath));
//初始化数据库
SystemFramework.Config.setConnection();
}
buyifly 2006-06-21
  • 打赏
  • 举报
回复
用户登陆信息我记在cookie中:(
可以吗?
TScom 2006-06-21
  • 打赏
  • 举报
回复
比如记录登录用户的信息,就可以记在SESSION中。
buyifly 2006-06-21
  • 打赏
  • 举报
回复
谢谢!
你们的网站是这么做的吗?
godwu 2006-06-21
  • 打赏
  • 举报
回复
Session.Item("xxx") = "aaaa"

用的时候取
例:dim strTemp as String = Session.Item("xxx")

而且SESSION可以存OBJECT类型

buyifly 2006-06-21
  • 打赏
  • 举报
回复
大伙都说用SESSOIN怎么做呢?:9
初学者, 不懂呀:(
ice_frank 2006-06-21
  • 打赏
  • 举报
回复
用Session吧
buyifly 2006-06-21
  • 打赏
  • 举报
回复
不是很明白:(
Small__Wolf 2006-06-21
  • 打赏
  • 举报
回复
static
buyifly 2006-06-21
  • 打赏
  • 举报
回复
不知道大伙做这类东东怎么搞的呢??
MonkWang 2006-06-21
  • 打赏
  • 举报
回复
存储在Session里就可以啊

62,074

社区成员

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

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

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

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