But the transition from server-side thinking to client-side thinking is the challenge. Often times, I can get the JQuery to do exactly what I want. But it needs to be dynamic in some form or fashion. Whether it's the number of rows to display, or the starting tab to display, I need to control that value from the server side.
I found a fairly easy way to specify JQuery parameters with a server-side variable. It turned out to be rather simple.
Let's say you're using the JQuery Accordion and you want establish the default accordion pane upon page load. In my case, I have 5 products I'm listing in a vertical fashion. And Each product is an accordion pane that contains the product details.
But, only 1 of the products is Featured, and I that's the accordion I want to be open by default on page load.
First, you set an inline variable in the .Net page like this:
Code: Select all
<script type="text/javascript">
$(function() {
$("#accordion").accordion({
header: "h3",
autoHeight: false,
clearStyle: true,
active: <%= this._ActiveIndex %>
});
});
</script>
In your code-behind, just specify this at the top where your variables are defined. Page_Load or Page_Init is always my first method in the code behind, so I declare variables right before either of those.
Code: Select all
public string _ActiveIndex = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
_ActiveIndex = "2";
}
I was pretty excited to get this working so well so I wanted to share. Enjoy