Hide the div sample code from the template page in the subpage

  • 2020-06-23 00:10:45
  • OfStack

The requirements are as follows:
1. The right side of the template page contains a login div, which is intended to be displayed when not logged in and hidden after logged in
2. Display a welcoming div, mainly to hide it through javascript

Note: RegisterClientScriptBlock cannot be used to register and execute javascrip in the template page.
So the registration and execution of javascript is done on the page page

Main.master template page
 
<!-- Log on to a small div--> 
<div class="loginDiv"> 
<div class="LoginDivTitle"> 
 Member login  
</div> 
<table class="loginTable"> 
<tr> 
<td class="LoginLabel"> The user name :</td> 
<td><input type="text" class="loginTxt" id="txtUserName" /></td> 
</tr> 
<tr> 
<td class="LoginLabel"> password :</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"> 
 dear <%=serverUserName %>, You are welcome ! 
</span> 
</div> 

< 1 > .Code in Main.master in the background
 
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 > Background code in the main page of MainPage, which is inherited from the template page 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 encapsulates code
 
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); 
} 

Related articles: