tags:

views:

64

answers:

4

Well, I have a really small problem. How can I access an ASP.NET Drop down list from jquery? I tried to use "CssClass" property of the drop down list to assign a CSS class and then accessing that list using that class, but later I found out that the class changes when an element is selected, so my initial class replaced by a new class.I set the "CssClass" property of that list to "InstrumentDropDown". When the page loads for the first time, its class property is set to "InstrumentDropDown", just as I expected. But when an element is selected, and a postback is made, it becomes something like "InstrumentDropDown sg selected" (may be there are underscore between words instead of spaces)

I also cannot use the "ClientID" property because the part of code that access that list is not inside the page, but in a separate javascript file.

So how can I access it ?

+1  A: 

The easiest thing I can think of is wrap your Drop Down List inside a element with another id. That way, you can reference it like this rather than worrying about ASP.NET ClientIDs.

Script:

$('#dropDown select');

Markup:

<div id="dropDown">
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
</div>
wsanville
Yup, that will certainly do the trick. It also came to my mind, but I wanted to know if there is any other way, without using that extra "div" just to access a list. Also I wanted to know the general practice.
Night Shade
If your not using MVC then the id selector will not work, due to naming containers. You should use class names instead of id's when working with Web Forms. Or, you could use the id selecting syntax that @Nick Craver used below.
Alexander
@xandercoded -- note the div is not `runat=server` so the name is not mangled.
tvanfosson
This will work in Web Forms, the div tag ID will remain unchanged because it is plain html (not runat="server").
wsanville
@tvanfosson your right, no mangling there. Never-mind my babbling...
Alexander
+2  A: 

When the page loads, use the class to find the dropdown box and store the id of the element in a variable.

 $(function() {
      var $ddl = $('select.myClass'); // get here

      $('.somethingelse').click( function() {
            $.get('...',function(data) {
                $ddl.html(data);  // use here
            }
      }
 });
tvanfosson
If I do that, then when the second time the "click" event fires, will "$ddl" contain that class value? If it is the case, then why? Isn't it a local variable which will be destroyed when the function execution is finished ?
Night Shade
$ddl is captured inside the click handler. Whenever the click handler is executed, it will use the value originally assigned to $ddl.
tvanfosson
Yup, that did the trick. Thank you very much. As far as I remember, this is the second time you saved me...... :)
Night Shade
+1  A: 

If there's only one of the dropdown on the page and you're sure of this, you can also find it by ID using the attribute ends-with selector ($=), like this:

$("[id$=dropDownID]")

This would for example find: <select id="MasterPage$Content$dropDownID">

Nick Craver
Nope, it is not the only drop down in the page.
Night Shade
@Sayem - There are multiple with this same ID? e.g. in various user controls?
Nick Craver
No, not with the same id, of course..... I thought you wanted to know if it is the only drop down in the page, regardless of the ID... :|
Night Shade
+1  A: 

In the case the CssClass name changes OnPostback this selector will work...

$(document).ready(function(){ 

   $('select.InstrumentDropDown_sg_selected, select.InstrumentDropDown').change(function(){
      var yourValue = $(this).val(); //retrieve value
      $(this).val('someValue'); //set value
   });

});
Alexander