在子頁中隱藏模板頁中的div示例代碼
更新時間:2013年08月30日 16:12:29 作者:
模板頁右邊包含了一個登陸div,想讓沒登陸的時候這個div顯示,登陸后該div隱藏,具體的實現(xiàn)如下,需要的朋友可以參考下
需求如下:
1.模板頁右邊包含了一個登陸div,想讓沒登陸的時候這個div顯示,登陸后該div隱藏
2.顯示一個歡迎用戶的div,主要是想通過javascript來隱藏
注意:模板頁里是不能使用RegisterClientScriptBlock注冊和執(zhí)行javascrip的,
所以javascript的注冊和執(zhí)行放在page頁中來實現(xiàn)了
Main.master模板頁里的內(nèi)容
<!--登錄小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);
}
1.模板頁右邊包含了一個登陸div,想讓沒登陸的時候這個div顯示,登陸后該div隱藏
2.顯示一個歡迎用戶的div,主要是想通過javascript來隱藏
注意:模板頁里是不能使用RegisterClientScriptBlock注冊和執(zhí)行javascrip的,
所以javascript的注冊和執(zhí)行放在page頁中來實現(xiàn)了
Main.master模板頁里的內(nèi)容
復(fù)制代碼 代碼如下:
<!--登錄小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中的代碼
復(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主頁面中后臺代碼,它是繼承于模板頁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);
}
相關(guān)文章
Asp.Net使用Bulk實現(xiàn)批量插入數(shù)據(jù)
這篇文章主要介紹了Asp.Net使用Bulk實現(xiàn)批量插入數(shù)據(jù)的方法,對于進行asp.net數(shù)據(jù)庫程序設(shè)計非常有借鑒價值,需要的朋友可以參考下2014-09-09
ASP.NET開發(fā)中經(jīng)常用到10款工具軟件介紹
從事.NET開發(fā)也好幾年了,工作過程中積累一些軟件工具,分享給大家,排名不分先后,希望對大家有所幫助。2016-04-04
HTML服務(wù)器控件和WEB服務(wù)器控件的區(qū)別和聯(lián)系介紹
學(xué)習(xí)asp.net的時候一會用Html服務(wù)器控件,一會用Web服務(wù)器控件,起初做起例子來也挺迷糊的,下面對這兩個控件研究了一下做個筆記在此與大家分享下,感興趣的朋友可以了解下2013-08-08
Asp.net實現(xiàn)無刷新調(diào)用后臺實體類數(shù)據(jù)并以Json格式返回
本文主要分享了Asp.net實現(xiàn)無刷新調(diào)用后臺實體類數(shù)據(jù)并以Json格式返回的具體實例方法,具有一定的參考價值,有需要的朋友可以看下2016-12-12
ASP.NET動態(tài)設(shè)置頁面標(biāo)題的方法詳解
這篇文章介紹了ASP.NET動態(tài)設(shè)置頁面標(biāo)題的方法詳解,有需要的朋友可以參考一下2013-07-07
如何在ASP.NET Core中使用ViewComponent
這篇文章主要介紹了如何在ASP.NET Core中使用ViewComponent,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04
.Net?WebApi中實現(xiàn)自動依賴注入的三種方法(最新推薦)
依賴關(guān)系注入 (DI) ,是一種軟件設(shè)計模式,這是一種在類及其依賴項之間實現(xiàn)控制反轉(zhuǎn) (IoC)?的技術(shù), .NET 中的依賴關(guān)系注入是框架的內(nèi)置部分,與配置、日志記錄和選項模式一樣,這篇文章主要介紹了.Net?WebApi中實現(xiàn)自動依賴注入的三種方法,需要的朋友可以參考下2024-03-03

