views:

45

answers:

2

I have a situation that I am stuck with, and hoping someone can help. I am building a .NET/C# web application in which I have a tabbed panel layout, and when the user clicks on each of the tabs the display panel is updated using javascript to hide and show some divs. None of these clicks cause postback, it is all client-side, so I can't use viewstate or session.

What I want to do is somehow remember which panel was last visible when the page is refreshed, yet without posting back to the server I am unsure how to do this. I have tried a hidden field but obviously its value is reset every time because the form is never submitted. I do know that I can achieve this using cookies but its a little annoying to implement for such a (seemingly) trivial operation ... but maybe this is the only way?

Does anyone have any more elegant solution to this problem?

Using a function like this to show and hide tabs):

function makeCurrent(tab) {

if (tab.title == 'Manage orders') {        
    document.getElementById('panelOrders').style.display = "block";
    // Hide others
    document.getElementById('panelAccounts').style.display = "none";
    document.getElementById('panelProducts').style.display = "none";
    document.getElementById('panelSettings').style.display = "none";

    // Remember last viewed panel
    document.getElementById('hdnCurrentlyViewing').value = "orders";
}

The panels are just divs with style.display controlling their visibility. Not sure if its useful to post HTML code because its fairly self explanatory ...?

A: 

You can make this happen without a postback is to make an AJAX call from Javascript where you tell your server what the current panel is as you switch it.

I prefer using a framework like JQuery or Prototype to help make these AJAX calls myself.

Dave Markle
I don't have much more use for a library like jQuery other than this functionality, so I'd rather go for something more lightweight. Guess I'll have to write some AJAX functions then?
Ryan
yeah, then you'd have to roll your own ajax functions. But IMO if you're doing effects like hiding divs and doing some UI manipulation in JS, JQuery is a great way to do this, and handles a lot of browser based annoyances automatically.
Dave Markle
A: 

I think Hidden field will be the best option. have you tried ASP:HiddenField? It can be accessed across postbacks.

But if you still have some reservations with postbacks and hiddenfield you can also use cookies from JS http://techpatterns.com/downloads/javascript_cookies.php this is helper lib for cookies manipulation within JS.

Regards.

Shoaib Shaikh
Yes I have tried a hidden field server control, but there are no postbacks occurring here (the display updates client side with javascript), so that doesn't work. The hidden field's value is never submitted to the server. I think what others have confirmed for me here is true - AJAX is the only method that will work.
Ryan
The cookies approach is an interesting one ... its really simple to add and remove cookies so maybe that would be easier than AJAX? However the cookies approach won't work for people with cookies disabled ...
Ryan
yes cookies are simple to implement but yet if someone has no support for cookies, it will be a problem..
Shoaib Shaikh