سؤال

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");
    }
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

You may be changing the class attribute but then you immediately redirect to another page so you never see it. When the page is reloaded, the change is lost.

You'll probably need to store the active page in a session variable and then when the control loads, set the active class.

[Edit] If you want the control to reflect what page it is on maybe you could use the title of the page (or one of the other Page attributes):

protected void Page_Load(object sender, EventArgs e)
{
    if (Parent.Page.Title == "My Title")
    {
    }

Or you can do either way check your brower's URL and make selectoion in your menu

string page = Path.GetFileNameWithoutExtension(Request.AppRelativeCurrentExecutionFilePath);
string pageDirectory = Path.GetDirectoryName(Request.AppRelativeCurrentExecutionFilePath);

string category = Request.QueryString.Count>0 ? Request.QueryString[0] : string.Empty;

switch (category)
{
    case "home":
        lnk_Home.CssClass = "selected";
        break;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top