<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> .aaa { position:relative; background-color:red; width:200px; height:50px; margin-left:5px; float:left; } </style> </head> <body> <form id="form1" runat="server"> <div id="Login" class="aaa"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>,欢迎你! <asp:Button ID="Button1" runat="server" Text="退出登陆" /> </div> <div id="noLogin" class="aaa"> <asp:HyperLink ID="HyperLink1" runat="server">登陆</asp:HyperLink> <asp:HyperLink ID="HyperLink2" runat="server">注册</asp:HyperLink> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click;//按钮事件 if (!IsPostBack) { Session["user"] = "张三";//判断用户是否登陆。若登陆 则显示张三 } //window.onload = function () 往外输出。。。 //都全部取出来 取两个 1:Login 2:noLogin Response.Write("<script>window.onload = function () {var oLog = document.getElementById(‘Login‘);var noLog = document.getElementById(‘noLogin‘);"); if (Session["user"] != null)//判断user是否为空,若不为空咋显示oLog,隐藏noLog。 { Response.Write("oLog.style.display = ‘block‘;noLog.style.display = ‘none‘;");//none隐藏block显示 Label1.Text = Session["user"].ToString(); } else//若不是 则相反 { Response.Write("oLog.style.display = ‘none‘;noLog.style.display = ‘block‘;"); } Response.Write("};</script>"); } void Button1_Click(object sender, EventArgs e) { Session["user"] = null; } }
时间: 2024-11-05 13:25:36