Reputation: 6758
I have a set of accordian divs (powered by the prototype library) in an asp.net page. I would like to save the state of the accordian, so when a postback event occurs, the same div is open rather than reloading the page entirely and opening the default div.
My plan was to set a page control value with the ID of the open div using the div's toggler click event function, and then fire a click event after postback calling that same div (using the value of the control holding the div ID) as the target. I'm not sure how to fire a javascript click event from vb.net however, and this sure seems like a lame workaround.
1) can you tell me how to fire a javascript click event from the vb.net codebehind page -or- 2) is there a more elgant way to do this?
Thanks
Upvotes: 0
Views: 2344
Reputation: 6986
julio, try this:
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "runFunction", "runFunction();" True)
That will run a JS function called "runFunction". I think your idea about storing the ID of the open div in a hidden field or something on the page is a good one. I think storing it in a cookie is unnecessary unless you want the state of the accordion to persist after the user leaves the page. Otherwise, you're forcing more data over the wire both ways on every request for someone who has that cookie.
Another Idea You could (and probably should) utilize AJAX. This would mean a very lightweight call to the server, and your accordion wouldn't reset itself because that part of the page wouldn't refresh.
Upvotes: 1
Reputation: 114447
You could use cookies. When the accordion state changes, re-write the cookie. When the page loads, load the data in the cookie and run a function to reset the accordian state.
It's probably a lot easier than messing with viewstate.
Upvotes: 0
Reputation: 37543
Since code behind is server side and JavaScript is client side, there is no way to perform this intermix. The best way I would think would be to have a control on the page used to maintain state in JavaScript that would survive the postback (for instance an asp:HiddenField). You could then set the id of the "open" div into this hidden field, and on the postback its value would survive in the viewstate.
Then you just have to add a small function to be called onload that goes into the hiddenfield, grabs the id and sets the appropriate panel. Given all of that, I'd recommend you investigate the usage of the MultiView control. This is precisely what this control was created to handle.
Upvotes: 1