Thursday, November 20, 2008

Basic jQuery Examples for ASP.NET Controls

Well it’s official - I’m the newest member of the jQuery fan club. Since Microsoft announced their partnership with the jQuery team and it’s integration with Visual Studio – I’ve been all over it.

I am amazed at how fast it’s taken off in the short time it’s been around. People are writing and publishing plugins like crazy.

As my first jQuery post, I thought I’d show a few basic examples of interacting with some of the common ASP.NET server controls.

DropDownList SelectedValue:

GET:   $('#<%=DropDownList1.ClientID%>').val()
SET: $('#<%=DropDownList1.ClientID%>').val("value_to_set")

DropDownList SelectedIndex (2 options):

GET: $('#<%=DropDownList1.ClientID%>').attr("selectedIndex")
GET: $('#<%=DropDownList1.ClientID%>')[0].selectedIndex
SET: $('#<%=DropDownList1.ClientID%>').attr("selectedIndex", "0")
SET: $('#<%=DropDownList1.ClientID%>')[0].selectedIndex = 0

RadioButtonList SelectedValue:

GET:   $("input[name='<%=RadioButtonList1.UniqueID%>']:checked").val()
SET: $("input[name='<%=RadioButtonList1.UniqueID%>'][value='value_to_set']").attr("checked", true)

Button Enable/Disable

$('#<%=Button1.ClientID%>').attr("disabled", true/false);

Label Text

GET:  $('#<%=Label1.ClientID%>').text()
SET: $('#<%=Label1.ClientID%>').text("value_to_set")

TextBox Text (also HiddenField Value)

GET:  $('#<%=TextBox1.ClientID%>').val()
SET: $('#<%=TextBox1.ClientID%>').val("value_to_set")

Check Visibility

if ($('#<%=Button1.ClientID%>').is(':visible'))

That’s it for now. More to come-