What is a good way to attach a client-side Jquery call inside the change event of a ASP dropdownlist?
I have read that the registerstartupscript is obsolete, but I do not see what is the better way to do that now. I want to mix JQ in the code behind so that it will be easier to maintain. Rather than doing a check inside of a JQ block to detect a change in the select and then calling toggle()
Is there a better way than this? What is the new way that replaces the obsolete register script methods?
protected void SearchDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
switch (SearchDropDown.SelectedItem.Value)
{
case "Category":
CategorySearch.Visible = true;
NameSearch.Visible = false;
SearchText.Visible = false;
Response.Write("<script type='text/javascript'>$('#category-container').toggle();</script>");
break;
case "Name":
NameSearch.Visible = true;
CategorySearch.Visible = false;
SearchText.Visible = false;
Response.Write("<script type='text/javascript'>$('#name-container').toggle();</script>");
break;
default:
SearchText.Visible = true;
NameSearch.Visible = false;
CategorySearch.Visible = false;
Response.Write("<script type='text/javascript'>$('#search-container').toggle();</script>");
break;
}
}
Do it all client-side in jQuery. I think that is the simplest way you could do it. I don’t think I’ve ever seen the mix of Javascript in code making things easier to maintain. In fact, I’d say it makes things harder to maintain.
Not to mention, doing all of this client-side will be faster and a better experience for the user to not have to have wait for a post-back/page load.