需求如下:
1.模板頁(yè)右邊包含了一個(gè)登陸div,想讓沒(méi)登陸的時(shí)候這個(gè)div顯示,登陸后該div隱藏
2.顯示一個(gè)歡迎用戶的div,主要是想通過(guò)javascript來(lái)隱藏
注意:模板頁(yè)里是不能使用RegisterClientScriptBlock注冊(cè)和執(zhí)行javascrip的,
所以javascript的注冊(cè)和執(zhí)行放在page頁(yè)中來(lái)實(shí)現(xiàn)了
Main.master模板頁(yè)里的內(nèi)容
復(fù)制代碼 代碼如下:
!--登錄小div-->
div class="loginDiv">
div class="LoginDivTitle">
會(huì)員登錄
/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>.在后臺(tái)Main.master中的代碼
復(fù)制代碼 代碼如下:
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主頁(yè)面中后臺(tái)代碼,它是繼承于模板頁(yè)Main.master的
復(fù)制代碼 代碼如下:
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封裝代碼
復(fù)制代碼 代碼如下:
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);
}