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-

6 comments:

  1. no example for setting RadioButtonList SelectedValue ?

    ReplyDelete
  2. philpill-
    Sorry, I completely overlooked the SET for the RadioButtonList. I've now added it to the list.

    Thanks-

    ReplyDelete
  3. Hello Kenneth,
    Thats great man, thanks for this valuable info.

    www.aspnetnova.blogspot.com

    ReplyDelete
  4. FYI...for RadioButtonList. Remove the @ symbol from "@value".

    The way listed above was marked deprecated in v1.2 and completely removed in v1.3. The correct way in v1.3 is to remove the @ from @value otherwise the rest is the same. We only noticed it after upgrading from v1.2 to v1.3 of JQuery.

    Timm Hagen

    ReplyDelete
  5. Hey Timm-
    Thanks for the info - I will update the post right now.

    Kenneth

    ReplyDelete