views:

182

answers:

3

Is there a way to get the selected value (or text, or index) of a select box that the server fills (using ASP.NET) in the client? I've tried $("#ID").val() - but the only time that works is when I continue to another page and then go back. Even then, the value that is returned is the previously selected value - if the user changes the selection, it's not registered unless they leave the page and go back. Go easy on me, I'm new at this...

Update: Tried using a regular html select, same issue (just with a populated entry). Let me elaborate on what I'm trying to do: in a separate page, I'm getting results for an autocomplete search box. The select boxes are for filters. Naturally, if the user selects a filter, I don't want to suggest items that are no longer valid. I'm looking for the keys in the ProcessRequest method of the page that contains autocomplete info.

public override void ProcessRequest(HttpContext context)
    {
        string respString;
        string qsKey = "q";
        Customer searchCust = Session[Data.Selected_Customer] as Customer;
        Dictionary<string, string> qsDict = context.Request.QueryString.ToDictionary(qsKey);

Shouldn't I get the results (for instance '&ID=foo' on the last line @ context.Request.QueryString?

A: 

are you sure that 'ID' is really the id of the select box ? been awhile since i used asp.net but i remember it mucked with identifiers a lot

Scott Evernden
#ID is a filler, but both name and id are correct when I view the page source.
lonehunter01
+1  A: 

If you want the actual selected item itself:

$("#ID option:selected");
Colin
You also need to add .text() if you want the value of course.
Sbm007
lonehunter01
I just ran a test and it worked fine for me. Are you including # with the id of the element? example: $("#myElementId option:selected").text();
DennyDotNet
A: 

I use the code (below) to manipulate an ASP.NET generated select box. Within the code I obtain the value of the original select box (item). Also consider using the .text() function.

 var setValue = function(item){ 
  //if value isn't already selected, postback
   if(input.value != item.text())
    if(select.attr('onchange'))
     eval(select[0].attributes['onchange'].nodeValue);

   //set input box value
   $(input).val(item.text());
   $(input).removeClass('empty');

   //select original ASP.NET select value (hidden)
   select.val(item.text());

   $(lookup).hide();

   //show type if present
   $('.selectType').show();
 };

Hope this helps.

pixelbobby