Response.Redirect, keep tabcontainer on active tab

3.7k Views Asked by At

My VB.net tabcontainer needs to stay on the active tab when I have each submit going through the response.redirect.

How can I achieve this? I need that response.redirect there because it will show what has been added in the main tab container.

<asp:TabContainer runat="server" ActiveTabIndex="0" Height="200px" 
Width="175px" ScrollBars="Auto" EnableTheming="True" Font-
Underline="False" ID="TabContainer2" EnableViewState="False" 
Style="float:right; padding-left: 110px; margin-bottom: 340px;" 
OnActiveTabChanged="TabContainer1_ActiveTabChanged">


Protected Sub TabContainer1_ActiveTabChanged(ByVal sender As Object, 
ByVal e As EventArgs)
    ViewState("ActiveTabIdx") = TabContainer1.ActiveTabIndex
End Sub


Protected Sub SubmitCompanies_Click(ByVal sender As Object, ByVal e 
As System.EventArgs) Handles SubmitCompanies.Click
*****there is more code here but for this question, it's not necessary so it has been 
omitted*****
    Response.Redirect(Request.RawUrl)
    ViewState("ActiveTabIdx") = TabContainer1.ActiveTabIndex


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
Handles Me.Load
ProductID.Value = Request.QueryString("id")
Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)
If Not ViewState("ActiveTabIdx") Is Nothing Then
    TabContainer1.ActiveTabIndex = Convert.ToInt32(Session("ActiveTabIdx"))
End If
End Sub
4

There are 4 best solutions below

5
On BEST ANSWER

ViewState won't work with Response.Redirect. With your current implementation, I would use QueryString or Session to store reference to the active tab.

Response.Redirect(String.Format("{0}?tab={1}", Request.RawUrl, TabContainer1.ActiveTabIndex)) 
3
On

Save the active tab index in say Session variable and in your Page_Load event check if Session("ActiveTabIdx") is not Nothing or empty then set the TabContainer1.ActiveTabIndex to the value of Session("ActiveTabIdx"). Something like this:

    Protected Sub SubmitCompanies_Click(ByVal sender As Object, ByVal e 
As System.EventArgs) Handles SubmitCompanies.Click
       'Rest of code
        Session("ActiveTabIdx") = TabContainer1.ActiveTabIndex
        Response.Redirect(Request.RawUrl)
    End Sub

    Protected Sub Page_Load(ByVal Sender As Object, ByVal e as System.EventArgs) Handles Page.Load
         If not ViewState("ActiveTabIdx") is Nothing Then
                TabContainer1.ActiveTabIndex = Convert.ToInt32(Session("ActiveTabIdx"))
         End If
    End Sub

Between in your SubmitCompanies_Click you are redirecting user before setting the ViewStat variable's value!

2
On

I think your solution will be to use jquery to save the selected tab on a cookie or anyplace when the user click on the tab and also to use jquery to get the old selected tab from the cookie and set the selected tab using jquery

3
On

As I can see you are assigning viewstate value

ViewState("ActiveTabIdx") = TabContainer1.ActiveTabIndex

after

Response.Redirect(Request.RawUrl)

This code is ureachable.

First note is that ViewState is not carried with new request (by calling Redirect method you start a new request). Solution is to use query string. Add active tab as a parameter at the end of your RawUrl and then read it on page load.

Hope this helps,

Kris