访问母版页中的其他页面时出现 <li>
appearing of an <li> when accessing other page in master page
当我使用用户名和密码登录时遇到问题,然后重定向到主页,登录按钮再次出现在所有页面中,因为在我注销之前它必须隐藏。所以我想要的是,当我单击登录按钮时,它会将我重定向到主页,但 <ul>
中的登录必须消失 我尝试将数据保存在 cookie 中,但它不起作用。
int a = 0;
string username = "";
string random = RandomString(26);
body,
div,
p {
margin: 20px;
background-color: #e410d5;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.clear {
clear: both;
}
.content {
width: 50%;
/* background-color: rgb(254,254,254);*/
border: 8px solid #ffd800;
border-radius: 15px 15px 15px 15px;
float: left;
background-color: #b314c5;
margin-left: 420px;
margin-top: 20px;
margin-bottom: 100px;
min-height: 220px;
position: absolute;
}
.menu {
/* background-color:rgb(10,110,178);*/
width: 60%;
margin-left: 590px;
padding: 0px;
height: 40px;
color: rgb(243, 243, 243);
border-radius: 5px 5px 5px 5px;
position: center;
margin-top: -7px;
}
.menu ul li {
float: left;
display: block;
list-style: none;
/*border-right: 1px solid rgb(10,85,125);
border-left: 1px solid rgb(67,153,253);*/
}
.menu ul li:hover {
background-color: #59058a;
border-right: 1px solid #e410d5;
}
.menu ul li a {
font-size: 13px;
font-weight: bold;
line-height: 40px;
padding: 8px 20px;
/*color:rgb(255,255,255);*/
text-decoration: none;
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="title" runat="server"></asp:ContentPlaceHolder>
</title>
<link href="Styles/StyleSheet.css" rel="stylesheet" />
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<style type="text/css">
.auto-style1 {
height: 241px;
}
</style>
</head>
<body>
<form id="form1" runat="server" class="auto-style1">
<div class="wrapper">
<div class="menu">
<nav>
<ul>
<li><a href="HomePage.aspx">Home</a>
</li>
<li id="loginlist" runat="server"><a href="LoginPage.aspx">Login</a>
</li>
<li><a href="ContactUs.aspx">Contact Us</a>
</li>
<li><a href="AboutUs.aspx">About Us</a>
</li>
</ul>
</nav>
</div>
<div class="clear"></div>
<div class="content">
<asp:ContentPlaceHolder id="contentbody" runat="server">
</asp:ContentPlaceHolder>
</div>
<div class="clear">
</div>
</div>
</form>
</body>
</html>
//////////// DECLRATIONS GOES HERE /////////////////////
using (SqlConnection sqlConnection = new SqlConnection("Data Source=(local);Database='Task_Management';Integrated Security=yes;"))
{
sqlConnection.Open();
string query = "Select Emp_ID FROM dbo.Employees where Emp_ID ='" + this.UserNameTextBox.Text +"'and Emp_Password='"+ this.PasswordTextBox.Text + "'";
using (SqlCommand sqlCommand = new SqlCommand(query, sqlConnection))
{
try
{
SqlDataReader readeenter code herer = sqlCommand.ExecuteReader();
if (reader.Read())
{
string treatment = reader[0].ToString();
username = treatment;
//Cookies for each user to be signed in or logout
Response.Cookies[username].Value = UserNameTextBox.Text;
Response.Cookies[username].Expires = DateTime.Now.AddDays(1);
Label2.Text = UserNameTextBox.Text;
if (Response.Cookies[username].Value == UserNameTextBox.Text)
{
message();
var ctrl = this.Master.FindControl("loginlist"); // loginlist is the id of the login <li> in the master page
ctrl.Visible = false;
Response.Redirect("HomePage.aspx");
}
// returns 1 if the loged in user is not an admin else it returns 0
a = 1;
}
}
catch (Exception f)
{
// Exception goes here
}
}
return a;
}
看看 LoginView
控件。这允许您根据某人是否登录或匿名显示不同的内容。
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<%-- Show Login option --%>
<ul>
<li><a href="HomePage.aspx">Home</a></li>
<li id="loginlist" runat="server"><a href="LoginPage.aspx">Login</a></li>
<li><a href="ContactUs.aspx">Contact Us</a></li>
<li><a href="AboutUs.aspx">About Us</a></li>
</ul>
</AnonymousTemplate>
<LoggedInTemplate>
<%-- Don't show Login option --%>
<ul>
<li><a href="HomePage.aspx">Home</a></li>
<li><a href="ContactUs.aspx">Contact Us</a></li>
<li><a href="AboutUs.aspx">About Us</a></li>
</ul>
</LoggedInTemplate>
</asp:LoginView>
在母版页中写一段js代码。
**注意下载 jquery.cookie 插件
$(document).read(function(i,val){
try
{
if($.cookie["username"]!="")
{
$("#loginlist").hide();
}
else
{
$("#loginlist").show();
}
}
catch($ms)
{
$("#loginlist").show();
}
});
这是因为使用了 Response.Redirect 方法。它会在当前页面执行结束之前向您发送另一个页面。还有另一个接受布尔值的重载,您必须向它发送 false。
Response.Redirect("HomePage.aspx",false);
但它仍然不起作用。我不知道为什么,但你可以在登录页面使用重定向方法并在主页隐藏控件。
在首页
var ctrl = this.Master.FindControl("loginlist");
ctrl.Visible = false;
在登录页面
Response.Redirect("HomePage.aspx");
更多信息
When Should I Use Response.Redirect(url, true)?
当我使用用户名和密码登录时遇到问题,然后重定向到主页,登录按钮再次出现在所有页面中,因为在我注销之前它必须隐藏。所以我想要的是,当我单击登录按钮时,它会将我重定向到主页,但 <ul>
中的登录必须消失 我尝试将数据保存在 cookie 中,但它不起作用。
int a = 0;
string username = "";
string random = RandomString(26);
body,
div,
p {
margin: 20px;
background-color: #e410d5;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.clear {
clear: both;
}
.content {
width: 50%;
/* background-color: rgb(254,254,254);*/
border: 8px solid #ffd800;
border-radius: 15px 15px 15px 15px;
float: left;
background-color: #b314c5;
margin-left: 420px;
margin-top: 20px;
margin-bottom: 100px;
min-height: 220px;
position: absolute;
}
.menu {
/* background-color:rgb(10,110,178);*/
width: 60%;
margin-left: 590px;
padding: 0px;
height: 40px;
color: rgb(243, 243, 243);
border-radius: 5px 5px 5px 5px;
position: center;
margin-top: -7px;
}
.menu ul li {
float: left;
display: block;
list-style: none;
/*border-right: 1px solid rgb(10,85,125);
border-left: 1px solid rgb(67,153,253);*/
}
.menu ul li:hover {
background-color: #59058a;
border-right: 1px solid #e410d5;
}
.menu ul li a {
font-size: 13px;
font-weight: bold;
line-height: 40px;
padding: 8px 20px;
/*color:rgb(255,255,255);*/
text-decoration: none;
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="title" runat="server"></asp:ContentPlaceHolder>
</title>
<link href="Styles/StyleSheet.css" rel="stylesheet" />
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
<style type="text/css">
.auto-style1 {
height: 241px;
}
</style>
</head>
<body>
<form id="form1" runat="server" class="auto-style1">
<div class="wrapper">
<div class="menu">
<nav>
<ul>
<li><a href="HomePage.aspx">Home</a>
</li>
<li id="loginlist" runat="server"><a href="LoginPage.aspx">Login</a>
</li>
<li><a href="ContactUs.aspx">Contact Us</a>
</li>
<li><a href="AboutUs.aspx">About Us</a>
</li>
</ul>
</nav>
</div>
<div class="clear"></div>
<div class="content">
<asp:ContentPlaceHolder id="contentbody" runat="server">
</asp:ContentPlaceHolder>
</div>
<div class="clear">
</div>
</div>
</form>
</body>
</html>
//////////// DECLRATIONS GOES HERE /////////////////////
using (SqlConnection sqlConnection = new SqlConnection("Data Source=(local);Database='Task_Management';Integrated Security=yes;"))
{
sqlConnection.Open();
string query = "Select Emp_ID FROM dbo.Employees where Emp_ID ='" + this.UserNameTextBox.Text +"'and Emp_Password='"+ this.PasswordTextBox.Text + "'";
using (SqlCommand sqlCommand = new SqlCommand(query, sqlConnection))
{
try
{
SqlDataReader readeenter code herer = sqlCommand.ExecuteReader();
if (reader.Read())
{
string treatment = reader[0].ToString();
username = treatment;
//Cookies for each user to be signed in or logout
Response.Cookies[username].Value = UserNameTextBox.Text;
Response.Cookies[username].Expires = DateTime.Now.AddDays(1);
Label2.Text = UserNameTextBox.Text;
if (Response.Cookies[username].Value == UserNameTextBox.Text)
{
message();
var ctrl = this.Master.FindControl("loginlist"); // loginlist is the id of the login <li> in the master page
ctrl.Visible = false;
Response.Redirect("HomePage.aspx");
}
// returns 1 if the loged in user is not an admin else it returns 0
a = 1;
}
}
catch (Exception f)
{
// Exception goes here
}
}
return a;
}
看看 LoginView
控件。这允许您根据某人是否登录或匿名显示不同的内容。
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<%-- Show Login option --%>
<ul>
<li><a href="HomePage.aspx">Home</a></li>
<li id="loginlist" runat="server"><a href="LoginPage.aspx">Login</a></li>
<li><a href="ContactUs.aspx">Contact Us</a></li>
<li><a href="AboutUs.aspx">About Us</a></li>
</ul>
</AnonymousTemplate>
<LoggedInTemplate>
<%-- Don't show Login option --%>
<ul>
<li><a href="HomePage.aspx">Home</a></li>
<li><a href="ContactUs.aspx">Contact Us</a></li>
<li><a href="AboutUs.aspx">About Us</a></li>
</ul>
</LoggedInTemplate>
</asp:LoginView>
在母版页中写一段js代码。 **注意下载 jquery.cookie 插件
$(document).read(function(i,val){
try
{
if($.cookie["username"]!="")
{
$("#loginlist").hide();
}
else
{
$("#loginlist").show();
}
}
catch($ms)
{
$("#loginlist").show();
}
});
这是因为使用了 Response.Redirect 方法。它会在当前页面执行结束之前向您发送另一个页面。还有另一个接受布尔值的重载,您必须向它发送 false。
Response.Redirect("HomePage.aspx",false);
但它仍然不起作用。我不知道为什么,但你可以在登录页面使用重定向方法并在主页隐藏控件。
在首页
var ctrl = this.Master.FindControl("loginlist");
ctrl.Visible = false;
在登录页面
Response.Redirect("HomePage.aspx");
更多信息
When Should I Use Response.Redirect(url, true)?