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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
masterpassConnect With a Mentor Commented:
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
All Courses

From novice to tech pro — start learning today.