在主页面中访问其他页面时出现<li>

时间:2016-05-24 13:58:17

标签: c# html asp.net webforms master-pages

当我使用用户名和密码登录时遇到问题,然后重定向到主页,登录按钮再次出现在所有页面中,因为必须隐藏登录按钮直到我退出。所以我想要的是当我点击loginbutton它将我重定向到主页但是<ul>中的登录必须消失我尝试在cookie中保存数据但它不起作用。

 int a = 0;
        string username = "";
        string random = RandomString(26);

&#13;
&#13;
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;
}
&#13;
<!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">

          &nbsp;&nbsp;&nbsp;

        </asp:ContentPlaceHolder>
      </div>
      <div class="clear">

      </div>


    </div>

  </form>
</body>

</html>
&#13;
&#13;
&#13;

        ////////////  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;
        }

3 个答案:

答案 0 :(得分:0)

查看LoginView控件。这允许您根据是否有人登录或匿名来显示不同的内容。

MSDN documentation

<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>

答案 1 :(得分:0)

在主页面中编写js代码。 **注意Dowload jquery.cookie插件

asset.fileURL

答案 2 :(得分:0)

这是因为使用了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)?