Search code examples
javascriptc#jqueryajaxcontroltoolkit

Show different tabs based on the link clicked


Scenario

I would like to display a particular tab of an ajax tab-container control when different links are clicked on a different page.

To clarify: Links are on page1.aspx and the ajax tab-container is on page2.aspx.

All links redirect to page2.aspx only.

But I need to display different tabs (on page2.aspx) according to different links clicked on previous page(page1.aspx)".

What I'm looking for

I thought I could do something along the following lines of pseudo-code:

LinkButton lnk1 = (LinkButton)PreviousPage.FindControl("btnLink1");
if (lnk1.Click)
{
  TabContainer1.ActiveTab = tab1;
}

The idea being that if a certain link has been clicked than a specific tab should be displayed.

I would put this in the PageLoad event of page2.aspx.

How can I achieve server-side (c#)? Or do I need a client-side (Javascript) method for that?

What I've tried

I've tried using session variables as suggested in one of the answers, but the click events are not firing.

Here's how I store the variables on page1.aspx on click of each linkbutton.

 protected void editPosition_Click(object sender, EventArgs e)
    {
        Session["LinkSelection"] = "lnkPosition";
    }

 protected void editRoom_Click(object sender, EventArgs e)
    {
        Session["LinkSelection"] = "lnkRoom";
    }

Then on PageLoad I do the checks on page2.aspx

 if (Session["LinkSelection"].ToString() == "lnkPosition")
   {
            TabContainer1.ActiveTab = PositionsTab;
   }

 if (Session["LinkSelection"].ToString() == "lnkRoom")
   {
            TabContainer1.ActiveTab = RoomsTab;
   }

The markup of the links is as follows:

<asp:LinkButton ID="editPosition" runat="server" OnClick="editPosition_Click" 
     Text="Position" ClientIDMode="Static" ></asp:LinkButton>
<asp:LinkButton ID="editRoom" runat="server" Text="Room" OnClick="editRoom_Click" 
     ClientIDMode="Static" ></asp:LinkButton>

Final edit

I have solved my issue by using query string parameters, instead of session variables.


Solution

  • I think, you have to try this.

    At page1.aspx, When Click on link , you have to set link selection value in a variable

    protected void lnkUser_Click(object sender, EventArgs e)
    {
        Session["LinkSelection"] = "lnkUser";
    } 
    
    protected void lnkCustomer_Click(object sender, EventArgs e)
    {
        Session["LinkSelection"] = "lnkCustomer";
    }
    

    At page2.aspx, on Page_Load Event, you have to check the session varribale value and then based on that, you have to visible true/false the Ajax tab

    if (Session["LinkSelection"] == "lnkUser")
     {
        //put your User tab Active/Enabled logic here 
     }
    else if (Session["LinkSelection"] == "lnkCustomer")
     {
        //put your Customer tab Active/Enabled logic here
     }
    

    Instead of using If else, you can also use Switch Case. This is the sample code. If you dont want to use Session variables then pass the value in querystring with page2.aspx such as Response.Redirect("page2.aspx?LinkSelection=lnkUser"). You can also send the encrypted querystring to page2 where you can decrypt it and process