Saba Aslam
Saba Aslam

Reputation: 41

dynamically change html control style using c#

I want to make a website where if admin login through admin.aspx I want to add one more navigation in my menu list. my menu list is made of <ul>...<li> i.e html control so how can I dynamically add the new

  • in my master page menu or else initially I add the admin menu and apply the style { visibility:hidden} and when the login is successful I want to change it to {visibility:visible }

    this is My master page 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 Default.aspx code

    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 is working fine but when I go back again to index.aspx the admin menu get hides automatically

    Upvotes: 4

    Views: 2480

  • Answers (3)

    jasim
    jasim

    Reputation: 459

    You have to use session and check the presence of session value on the top of your master page. while you are going back to the master page, it again checks for the session value and then you can write css code to set the admin menu as visible.

    Technically, step by step procedure:

    1. create session on second page (default.aspx)
    2. check for session value on top of master page: put an if condition, if session value is present write code to show admin menu in it.
    3. create a log-out page to invalidate the session.

    you can read more about ASP sessions here: http://www.w3schools.com/asp/asp_sessions.asp

    Upvotes: 0

    Shay
    Shay

    Reputation: 1768

    Saba, you're doing it wrong.

    I can think of 2 possible simple ways to accomplish that:

    1. Setting the web control Visible property

    In the master page code behind (MasterPage.master.cs) add this line to the OnLoad or Page_Load method:

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

    In the MasterPage.master add runat="server" to your <li> tag.

    2. Adding the <li> Programmatically

    In the MasterPage.master add runat="server" to your <ul> tag.

    And in the master page code behind add to the OnLoad or 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);
    }
    

    Upvotes: 0

    LauraB
    LauraB

    Reputation: 21

    From your code, I am guessing that the line:

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

    is some form of saved value on the login process. If this is the case and you have some hidden input field or something - then every time your page loads () then call a method that sets the Admin menu. Change the "if" code based on what kind of remembered login value you have working on the site. (Session or viewstate or cookie)

    Example:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              setAdminMenu();
            }
        }
    
     private void setAdminMenu()
        {
          if(f.pass.Equals(txtpass.Value))
          {
            abc.Visibility = visible;
          }
        }
    

    Upvotes: 2

    Related Questions