0

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
Jamie
  • 1,579
  • 8
  • 34
  • 74

4 Answers4

1

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)) 
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • I tried that and got an error message saying input string was not in the correct format. – Jamie Oct 05 '11 at 16:42
  • Hmm... try again. I was using the wrong ID for the tab container. – James Johnson Oct 05 '11 at 16:46
  • Oh Visual Studio caught that and I changed it to TabContainer1, sorry I forgot to mention that. Still says same error message unfortunately. – Jamie Oct 05 '11 at 16:51
  • I don't think there's any problem with this code... What it the output of `RawUrl`? It shouldn't matter, but try changing the second argument to: `TabContainer1.ActiveTabIndex.ToString()`. Also, you know that you'll have to add some logic to select the tab when the page loads right? – James Johnson Oct 05 '11 at 16:56
  • I edited the original post to show what I have on page load. I'm not too sure about the tabcontainer, I just followed a tutorial I found and hoped that it worked right. But it didn't show me how to keep tabs active when I refresh. – Jamie Oct 05 '11 at 17:12
0

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!

Waqas
  • 6,812
  • 2
  • 33
  • 50
  • Where would I saw the ActiveTabIndex at? In the codebehind? Visual Studio isn't recognizing getIndexofActiveTab on my vb page. – Jamie Oct 05 '11 at 16:15
  • in your SubmitCompanies_Click event before calling redirect add this piece of code Session("ActiveTabIdx") = TabContainer1.ActiveTabIndex – Waqas Oct 05 '11 at 16:19
  • It seems like it would work but when I submit a new company, it still refreshes and the tab goes back to the very first one. – Jamie Oct 05 '11 at 16:32
0

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

Samir Adel
  • 2,691
  • 2
  • 15
  • 16
  • I would really rather do this in ASP.net since I don't know a lot about jquery and find it pretty complicated. – Jamie Oct 05 '11 at 16:28
  • If there is a jquery tutorial that sounds like my situation, I would be happy to read through it. I just hate javascript though. I don't understand it, and it looks so messy – Jamie Oct 05 '11 at 18:05
0

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

Drakkonite
  • 507
  • 3
  • 9
  • I tried `Response.Redirect(Request.RawUrl(ViewState("ActiveTabIdx") = TabContainer1.ActiveTabIndex))` and my page goes to a completely different page on the site. – Jamie Oct 05 '11 at 16:12
  • You need to append a new query string parameter to your RawUrl Response.Redirect(Request.RawUrl + "?activeTab=" + TabContainer1.ActiveTabIndex) and then pick this value using Request.QueryString("activeTab") – Drakkonite Oct 05 '11 at 16:20
  • There is an error with the Request code saying `Property access must assign to the property or use its value.` I tried using that before the Response.Redirect and after it and it says the same thing. – Jamie Oct 05 '11 at 16:30