ASP.NET - Use OnClientActiveTabChanged to remember selected tab, restore on postback/pageload

I have an ajax TabContainer in my asp.net (VB) app.  Initially I was just using the TabContainer1_ActiveTabChanged sub to store which tab was selected.  However, it only does that if you set it to do a postback anytime the user changes the tab.  That causes an unnecessary refresh, so I don't want it to postback on a tabchange.  That pretty much leaves me with using a javascript function with OnClientActiveTabChanged, right?  I've played with that quite a bit but just can't get it right.  It looks like setting a cookie would be the best way to go.  I found the attached code to set a cookie, but I'm not sure how to call it with the active tab... It would be something like OnClientActiveTabChanged="SetCookie('ActiveTab',varNewTab,30)" but I really have no idea how to get "varNewTab".
function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }

Open in new window

LVL 1
CerixusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

masterpassCommented:
use this script

call the script like this

OnClientActiveTabChanged="clientActiveTabChanged"
<script language="javascript" type="text/javascript">
   function clientActiveTabChanged(sender, args) {
      var ActiveTab = sender.get_activeTabIndex() ;
	  var expiredays =30;
	  var c_name="ActiveTab";
	  var exdate=new Date();
      exdate.setDate(exdate.getDate()+expiredays);
      document.cookie=c_name+ "=" +escape(ActiveTab)+
      ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
      }
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CerixusAuthor Commented:
That works great!  But I'd like to sneak a followup question in here if I can...  If I switch pages, it retains the cookie.  I want it to go back to the default tab.  So in my Page_Load of the search/list page I'm trying to set a cookie via ASP.NET like this:

        Dim aCookie As New HttpCookie("ActiveTab")
        aCookie.Value = "1"
        aCookie.Expires = DateTime.Now.AddDays(30)
        Response.Cookies.Add(aCookie)

When I visit the search page and select a new app, it still remembers the tab from the previous app (when the javascript function set the cookie).

What am I missing?
0
CerixusAuthor Commented:
I also tried

Response.Cookies.Add(New HttpCookie("ActiveTab", "1"))

and

Response.Cookies("ActiveTab").Value = "1"
Response.Cookies("ActiveTab").Expires = DateTime.Now.AddDays(30)

It still doesn't seem to overwrite the cookie set by the javascript function :(
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

CerixusAuthor Commented:
And I tried deleting it...

        If (Not Request.Cookies("ActiveTab") Is Nothing) Then
            Dim myCookie As HttpCookie
            myCookie = New HttpCookie("ActiveTab")
            myCookie.Expires = DateTime.Now.AddDays(-1D)
            Response.Cookies.Add(myCookie)
        End If
0
masterpassCommented:
Instead of

Dim aCookie As New HttpCookie("ActiveTab")
        aCookie.Value = "1"
        aCookie.Expires = DateTime.Now.AddDays(30)
        Response.Cookies.Add(aCookie)

have

TabPanel1.ActiveTabIndex = 1;
0
CerixusAuthor Commented:
But the tabpanel doesn't exist on that page.

Basically I have a "View" page that views a record in the database, with multiple tabs.  When you switch tabs, it stores the active tab to a cookie.  That way, if you refresh the page or edit something, it returns to the tab you selected.  Now, what happens if you go back to the index or the search page and then view a new record?  It goes back to the tab you were last on in the previous record.  I don't want that.

So, in the "Search" page and "Index" page I'm trying to overwrite the cookie, that way it ensures any time they view a new record (short of manually typing in the new URL) it resets the cookie to the default tab index.
0
CerixusAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.