How to use javascript with an dropdownlist control?

By : Greg

I don't currently use though I would be open to it if it is the only solution. I have a auto-complete control on screen that I am using to populate a dropdownlist with values through javascript (jQuery). I have had to use EnableEventValidation="false" to allow this. After I add my options to the select and the form is posted back I would like to be able to get all the values for the option elements I have added to the dropdownlist through javascript.. Is there a good way to accomplish this?

By : Greg


You can get the selected value directly from the form like so:

string fooBar = Request.Form[SomeDropDown.UniqueID];

This will return the correct value no matter what you do to to the drop down options. I use javascript to change the quantity dropdown for a product based on size selection for reflecting product availability.

By : Jason

If a DropDownList leaves the server with no options, it's recreated server-side with no options (from the viewstate)

You could add the options to a hidden html control as a delimited string in your javascript as well as to the select list. Then iterate that into the control once server-side on post-back. Otherwise you could ajax them to the server and re-render the DropDownList only for each addition.

As far as is concerned, a query that returns zero rows is not an error. If your application wishes to treat such a query as an error, you should create your own exception class by inheriting from Exception.

public class myException : Exception
   public myException(string s) : base() 
      this.MyReasonMessage = s;

public void GetData(int identifier)
    dataAdapter.Command.Text = "Select * from table1 Where ident = " + identifier.toString();
    DataSet ds = dataAdapter.Fill(ds);
    if (ds.table1.Rows.Count == 0)
        throw new myException("Data not found");

This video can help you solving your question :)
By: admin