Dynamically change html management style using C #

I want to create a website where, if the administrator logs in via admin.aspx I want to add another navigation to my menu list. my menu list consists of <ul>...<li>ie html control since I can dynamically add new ones

in my main page menu or else first I add an admin menu and apply style { visibility:hidden} and when the login is successful, I want to change it to{visibility:visible }

this is my homepage code

<ul id="ul_myLst" runat="server">
    <li><a href="Testimonials.aspx">Testimonial</a>
    </li>
    <li><a href="#fakelink">Contact Us</a>
    </li>
    <li><a href="#fakelink">About Us</a>
    </li>
    <li><a href="Registration.aspx">Registartion</a>
    </li>
    <li><a href="OurFaculty.aspx">Our Faculty</a>
    </li>
    <li id="abc" runat="server" style="visibility:hidden">
        <a href="OurFaculty.aspx">Admin</a>
    </li>
</ul>

and this is my code Default.aspx

if (f.pass.Equals(txtpass.Value)) {

    HtmlGenericControl ul = (HtmlGenericControl)(this.Master.FindControl("abc"));
    //ul.Attributes["class"] = "admin-p";
    ul.Style.Remove("visibility");
    ul.Style["visibility"] = "visible";

    Response.Redirect("Index.aspx");

}

this code works fine, but when I go back to index.aspx, the admin menu is automatically hidden

+4
source share
3 answers

, , :

if(f.pass.Equals(txtpass.Value))

- . , - - - , (), , . "if" , . (Session viewstate cookie)

:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          setAdminMenu();
        }
    }

 private void setAdminMenu()
    {
      if(f.pass.Equals(txtpass.Value))
      {
        abc.Visibility = visible;
      }
    }
+2

. , , css, .

, :

  1. (default.aspx)
  2. : if, , , .
  3. .

ASP : http://www.w3schools.com/asp/asp_sessions.asp

0

, .

:

1. - Visible

(MasterPage.master.cs) OnLoad Page_Load:

var liAdmin  = (HtmlGenericControl)Page.FindControl("abc");
liAdmin.Visible  = User.IsInRole("Admin");

MasterPage.master runat="server" <li>.

2. <li>

MasterPage.master runat="server" <ul>.

OnLoad Page_Load:

if(User.IsInRole("Admin"))
{
    var ulMenu  = (HtmlGenericControl)Page.FindControl("ul_myLst");
    var liAdmin = new HtmlGenericControl("li");
    var a = new HtmlAnchor();
    a.HRef = "OurFaculty.aspx";
    a.InnerText = "Admin";
    liAdmin.Controls.Add(a);
    ulMenu.Controls.Add(liAdmin);
}
0

All Articles