需求如下:
1.模板页右边包含了一个登陆div,想让没登陆的时候这个div显示,登陆后该div隐藏
2.显示一个欢迎用户的div,主要是想通过javascript来隐藏
注意:模板页里是不能使用RegisterClientScriptBlock注册和执行javascrip的,
所以javascript的注册和执行放在page页中来实现了
Main.master模板页里的内容
<!--登录小div-->
<div class="loginDiv">
<div class="LoginDivTitle">
会员登录
</div>
<table class="loginTable">
<tr>
<td class="LoginLabel">用户名:</td>
<td><input type="text" class="loginTxt" id="txtUserName" /></td>
</tr>
<tr>
<td class="LoginLabel">密码:</td>
<td><input type="password" class="loginTxt" id="txtPass" /></td>
</tr>
<tr>
<td class="LoginTdButtons" colspan="2">
<input src="../images/az-login-gold-3d.gif" type="image" id="btnLogin" />
<input src="../images/az-newuser-gold-3d.gif" type="image" id="btnReg" />
</td>
</tr>
</table>
</div>
<div class="loginOkDiv" style="display:none">
<span class="spanLoginOk" id="spanUserInfo">
尊敬的<%=serverUserName %>,欢迎你光临!
</span>
</div>
<1>.在后台Main.master中的代码
protected string serverUserName;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Model.Users user = Session["currUser"] as Model.Users;
if (user != null)
{
serverUserName = user.Name;
}
}
}
<2>MainPage主页面中后台代码,它是继承于模板页Main.master的
public partial class MainPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Model.Users user = Session["currUser"] as Model.Users;
if (user != null)
{
common.CommonCode.ExecuteScriptFunc(this,true);
}
else
{
common.CommonCode.ExecuteScriptFunc(this,false);
}
}
}
}
<3>ExecuteScriptFunc封装代码
public static void ExecuteScriptFunc(System.Web.UI.Page page, bool bShowUserInfo)
{
string func = "function showUser(isLogin){/r/n/r/nif (isLogin) {/r/n" +
"$(/".loginDiv/").hide();/r/n" +
"$(/".loginOkDiv/").show();/r/n" +
"}/r/n" +
"else {/r/n" +
"$(/".loginDiv/").show();/r/n" +
"$(/".loginOkDiv/").hide();/r/n" +
"}}";
string func1 = "";
if (bShowUserInfo)
{
func1 = func + "/r/n" +
"$(function(){/r/nshowUser(true)" +
"});";
}
else
{
func1 = func + "/r/n" +
"$(function(){/r/nshowUser(false)" +
"});";
}
page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(),
func1, true);
//page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(),
// func1);
}