我正在开发网络应用程序。在此应用程序中,用户需要登录才能执行某些任务。我已经制作了我的自定义登录屏幕。我想让 site.master 的登录状态在用户登录后立即更新,但坦率地说,我不知道该怎么做。
下面的代码取自 login.aspx.cs。当用户单击登录按钮时,将执行以下代码。但我不知道我可以在 site.master 中更新登录状态(包含图片)
protected void Button1_Click(object sender, EventArgs e)
{
string user = username.Text;
string pass = password.Text;
Session["firstName"] = username.Text;
localhost.UserRegistration n = new localhost.UserRegistration();
if (n.checkUser(user, pass))
{
Response.Redirect("UserProfile.aspx");
}
else
{
message1.Text = "UserName or Password is Wrong. Please check your details and login again.";
}
请单击此处查看图片,以便有人可能对此有所了解。
站点主代码
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="SystemSoftware.SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<div class="page">
<div class="header">
<div class="title">
<h1>
My ASP.NET Application
</h1>
</div>
<div class="loginDisplay">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ <a href="~/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
[ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
</LoggedInTemplate>
</asp:LoginView>
</div>
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/Create Account.aspx" Text="Create Account"/>
</Items>
</asp:Menu>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
</html>
站点.Master.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SystemSoftware
{
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["firstName"] != null)
{
HeadLoginView.Visible = false;
}
else
{
HeadLoginView.Visible = true;
}
}
}
}