Search code examples
c#htmlasp.netwebformsmaster-pages

appearing of an <li> when accessing other page in master page


I'm facing a problem when I login with a username and a password then, redirected to the homepage the login button appears again in all pages where as it must be hidden until I logout. so what I want is when I click loginbutton it redirects me to the homepage but the login in <ul> must be disappeared I tried saving data in a cookie but it doesn't work.

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

          &nbsp;&nbsp;&nbsp;

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

Solution

  • Write a js code in your master page. **Note Dowload jquery.cookie plugins

    $(document).read(function(i,val){
      try
      {
         if($.cookie["username"]!="")
         {
             $("#loginlist").hide();
         }
         else
         {
             $("#loginlist").show();
         }
      }
      catch($ms)
     {
        $("#loginlist").show();
     }
    });