c# 在.cs里可以调用Global.asax里的数据吗,怎么调用?
3个回答
展开全部
例如在网站根目录下的Global.asax中写入下代码
void Application_Start(object sender, EventArgs e)
{
Application["UserNameLengthLimit"] = 50;//设置注册的用户名其长度不得超过50个数字\字母或汉字
}
则在具体的cs文件中调用如下
protected void hkCreateUserWizard_CreatingUser(object sender, LoginCancelEventArgs e)
{
int hbInt32_temp = Convert.ToInt32( Application["UserNameLengthLimit"]);
if (hkCreateUserWizard.UserName.Length > hbInt32_temp)
{
e.Cancel = true;
hkLabel1.Text = "用户名长度不能超过" + Application["UserNameLengthLimit"].ToString() ;
}
}
-----------------------
在上面,完成了调用Global.asax中的数据
------------又或者如下--------------------------------------------
Global.asax内容如下:
namespace WebApplication1
{
public class Global : System.Web.HttpApplication
{
public static string UserName;
protected void Application_Start(object sender, EventArgs e)
{
string User = "MyName";
UserName = User;
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
Default.aspx内容如下:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Global.UserName);
}
}
}
void Application_Start(object sender, EventArgs e)
{
Application["UserNameLengthLimit"] = 50;//设置注册的用户名其长度不得超过50个数字\字母或汉字
}
则在具体的cs文件中调用如下
protected void hkCreateUserWizard_CreatingUser(object sender, LoginCancelEventArgs e)
{
int hbInt32_temp = Convert.ToInt32( Application["UserNameLengthLimit"]);
if (hkCreateUserWizard.UserName.Length > hbInt32_temp)
{
e.Cancel = true;
hkLabel1.Text = "用户名长度不能超过" + Application["UserNameLengthLimit"].ToString() ;
}
}
-----------------------
在上面,完成了调用Global.asax中的数据
------------又或者如下--------------------------------------------
Global.asax内容如下:
namespace WebApplication1
{
public class Global : System.Web.HttpApplication
{
public static string UserName;
protected void Application_Start(object sender, EventArgs e)
{
string User = "MyName";
UserName = User;
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
Default.aspx内容如下:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Global.UserName);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询