change class of li from server side in custom control

skhurams picture skhurams · Apr 22, 2013 · Viewed 6.9k times · Source

Hi i have created a server control which consists of <UL> and its <LI>
I just wanted to reuse my menu in all six pages
below is controls code Asp Code

<%@ Control Language="C#" ClassName="Menu" %>
     <ul id="ulSideBar" class="nav nav-list" runat="server">
                <li accesskey="1" id="liDefault" runat="server" **class="active"**>
                    <asp:LinkButton runat="server" ID="lnkDefault" OnClick="lnkDefault_Click">Introduction</asp:LinkButton></li>
                <li accesskey="2" id="liSquad" runat="server">
                    <asp:LinkButton runat="server" ID="lnkSquad" OnClick="lnkSquad_Click">Squad</asp:LinkButton>
                </li>
                <li accesskey="3" id="liGallery" runat="server">
                    <asp:LinkButton runat="server" ID="lnkGallery" OnClick="lnkGallery_Click">Gallery</asp:LinkButton>
                </li>
                <li accesskey="4" id="liMatches" runat="server">
                    <asp:LinkButton runat="server" ID="lnkMatches" OnClick="lnkMatches_Click">Matches</asp:LinkButton>
                </li>
                <li accesskey="5" id="liActivities" runat="server">
                    <asp:LinkButton runat="server" ID="lnkActivities" OnClick="lnkActivities_Click">Activities</asp:LinkButton>
                </li>
                <li accesskey="6" id="liNewsFeed" runat="server">
                    <asp:LinkButton runat="server" ID="lnkNewsFeed" OnClick="lnkNewsFeed_Click">News Feed</asp:LinkButton>
                </li>
            </ul>

Im using this control in my six pages Now my question is how can i change class=active whichever <li> I click, only through server side

I have tried this code in Control's codebehind but its not working

 protected void lnkDefault_Click(object sender, EventArgs e)
    {
        liDefault.Attributes.Add("class", "active");
        liSquad.Attributes.Remove("class");
        liGallery.Attributes.Remove("class");
        liMatches.Attributes.Remove("class");
        liActivities.Attributes.Remove("class");
        liNewsFeed.Attributes.Remove("class");
        Response.Redirect("Default.aspx");
    }
    protected void lnkSquad_Click(object sender, EventArgs e)
    {
        liDefault.Attributes.Remove("class");
        liSquad.Attributes.Add("class", "active");
        liGallery.Attributes.Remove("class");
        liMatches.Attributes.Remove("class");
        liActivities.Attributes.Remove("class");
        liNewsFeed.Attributes.Remove("class");
        Response.Redirect("Squad.aspx");
    }
    protected void lnkGallery_Click(object sender, EventArgs e)
    {
        liSquad.Attributes.Remove("class");
        liDefault.Attributes.Remove("class");
        liGallery.Attributes.Add("class", "active");
        liMatches.Attributes.Remove("class");
        liActivities.Attributes.Remove("class");
        liNewsFeed.Attributes.Remove("class");
        Response.Redirect("Gallery.aspx");
    }
    protected void lnkMatches_Click(object sender, EventArgs e)
    {
        liDefault.Attributes.Remove("class");
        liSquad.Attributes.Remove("class");
        liGallery.Attributes.Remove("class");
        liMatches.Attributes.Add("class", "active");
        liActivities.Attributes.Remove("class");
        liNewsFeed.Attributes.Remove("class");
        Response.Redirect("Matches.aspx");
    }
    protected void lnkActivities_Click(object sender, EventArgs e)
    {
        liDefault.Attributes.Remove("class");
        liSquad.Attributes.Remove("class");
        liGallery.Attributes.Remove("class");
        liMatches.Attributes.Remove("class");
        liActivities.Attributes.Add("class", "active");
        liNewsFeed.Attributes.Remove("class");
        Response.Redirect("Activities.aspx");
    }
    protected void lnkNewsFeed_Click(object sender, EventArgs e)
    {
        liDefault.Attributes.Remove("class");
        liSquad.Attributes.Add("class", "active");
        liGallery.Attributes.Remove("class");
        liMatches.Attributes.Remove("class");
        liActivities.Attributes.Remove("class");
        liNewsFeed.Attributes.Add("class", "active");
        Response.Redirect("NewsFeed.aspx");
    }

Answer

skhurams picture skhurams · Apr 23, 2013

This solved my problem. My problem was i was using a custom control having <ul> <li> but the page where the control was used had a problem ie when user click li it should change its class to active (class='active') but due to post back it was not changing
May be if someone else has this problem can use my solution. I used this function in my custom control codebehind

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // switch (Parent.TemplateControl.AppRelativeVirtualPath.Replace("~/Teams/", ""))
            switch (System.IO.Path.GetFileName(Page.Request.Path))
            {
                case "Default.aspx":
                    liDefault.Attributes.Add("class", "active");
                    liSquad.Attributes.Remove("class");
                    liGallery.Attributes.Remove("class");
                    liMatches.Attributes.Remove("class");
                    liActivities.Attributes.Remove("class");
                    liNewsFeed.Attributes.Remove("class");
                    break;
                case "Squad.aspx":
                    liDefault.Attributes.Remove("class");
                    liSquad.Attributes.Add("class", "active");
                    liGallery.Attributes.Remove("class");
                    liMatches.Attributes.Remove("class");
                    liActivities.Attributes.Remove("class");
                    liNewsFeed.Attributes.Remove("class");
                    break;
                case "Gallery.aspx":
                    liSquad.Attributes.Remove("class");
                    liDefault.Attributes.Remove("class");
                    liGallery.Attributes.Add("class", "active");
                    liMatches.Attributes.Remove("class");
                    liActivities.Attributes.Remove("class");
                    liNewsFeed.Attributes.Remove("class");
                    break;
                case "Matches.aspx":
                    liDefault.Attributes.Remove("class");
                    liSquad.Attributes.Remove("class");
                    liGallery.Attributes.Remove("class");
                    liMatches.Attributes.Add("class", "active");
                    liActivities.Attributes.Remove("class");
                    liNewsFeed.Attributes.Remove("class");
                    break;
                case "Activities.aspx":
                    liDefault.Attributes.Remove("class");
                    liSquad.Attributes.Remove("class");
                    liGallery.Attributes.Remove("class");
                    liMatches.Attributes.Remove("class");
                    liActivities.Attributes.Add("class", "active");
                    liNewsFeed.Attributes.Remove("class");
                    break;
                case "NewsFeed.aspx":
                    liDefault.Attributes.Remove("class");
                    liSquad.Attributes.Remove("class");
                    liGallery.Attributes.Remove("class");
                    liMatches.Attributes.Remove("class");
                    liActivities.Attributes.Remove("class");
                    liNewsFeed.Attributes.Add("class", "active");
                    break;
            }
        }
    }