Hide the div sample code from the template page in a child page _ practical Tips

Source: Internet
Author: User
requirements are as follows:
1. Template page to the right contains a landing div, want to let not log in when this div display, after landing the Div hidden
2. Display a welcome user Div, mainly want to hide through JavaScript

Note: You cannot use RegisterClientScriptBlock to register and execute Javascrip in a template page.
So the registration and execution of JavaScript is done on the page page.

content in main.master template page
Copy Code code as follows:

<!--login Small div-->
<div class= "Logindiv" >
<div class= "Logindivtitle" >
Member Login
</div>
<table class= "Logintable" >
<tr>
&LT;TD class= "Loginlabel" > Username:</td>
<td><input type= "text" class= "Logintxt" id= "txtUserName"/></td>
</tr>
<tr>
&LT;TD class= "Loginlabel" > Password:</td>
<td><input type= "Password" class= "Logintxt" id= "Txtpass"/></td>
</tr>
<tr>
&LT;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%&gt, Welcome to your visit!
</span>
</div>

<1> code in the background main.master
Copy Code code as follows:

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 The background code in the main page, which is inherited from the template page Main.master
Copy Code code as follows:

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 Package Code
Copy Code code as follows:

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\ "). Sh ow (); \ r \ n "+
" $ (\ ". loginokdiv\"). hide (); \ r "+
"} ";
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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.