Xaisoft
Xaisoft

Reputation: 46591

Get the active index of the jquery accordion pane from asp.net on the server-side?

How can I get the active index of the jquery accordion pane when a button is clicked? What I want to do is when a button is clicked in pane 3 for example, I want to store that value and when the page is reloaded, I want pane 3 to remain open.

I intially had this in my server side click and when I hard code a value in for paneIndex it works fine, but obviously, I don't want to do this, I want to get the index on the click and pass that to the script.

string script = "<script type=\"text/javascript\">var paneIndex = " + 3 + "</script>";

if(!ClientScript.IsStartupScriptRegistered("JSScript"))
    ClientScript.RegisterStartupScript(this.GetType(),"JSScript", script);

Upvotes: 2

Views: 4862

Answers (3)

Tim Geerts
Tim Geerts

Reputation: 321

What's also a possibility is using the jquery.cookie plugin and storing the active pane index in a cookie. That way, everything actually happens clientside. I don't know if this might be a valid answer, just throwing it out here for completeness sake :D

Upvotes: 0

Bryan Batchelder
Bryan Batchelder

Reputation: 3637

You will want to bind a function to the change event of the accordian, and store the new active index into a hidden input so that it gets sent back to the server.

As far as round-tripping the active index back to the HTML that is returned from the server - your approach is fine. Obviously instead of the hardcoded value of 3, you would put in the value from the hidden input.

$("#accordion").accordion({
    active:1,
    change: function(event, ui) {
        var activeIndex = $("#accordion").accordion('option','active');
        $("myHiddenInputId").val(activeIndex);
        //alert(activeIndex);
    }
});

From the server side, you can access the value and push it back to the page in a similar manner as you posted in the question:

string activeIndex = Request.Form["myHiddenInputName"];
string script = string.Format(@"<script type=""text/javascript"">var paneIndex = {0};</script>", activeIndex);

That should do it.

Upvotes: 0

Morcalavin
Morcalavin

Reputation: 152

You could store the value in a hidden form field, and assuming you are doing a postback, that information will now be in the hidden field for you to use on the server side.

Upvotes: 1

Related Questions