c# - Populating a dropdownlist based on a previous dropdownlist selection -


I have been given two dropdown lists within my ASP website, there are four options in the first one that a user chooses and its basis The second dropdown list will pop up from the selected database column data on

This code currently has two problems:

1. Under my 'obsolete index change' method, I can not trigger my event if I want to click the first element in the dropdown list, then I have to click on another item and it will also trigger again for the first time. I have autopostback set to true

2 When the event triggers the data, the second dropdown list is not populated, for this dataset Popular with the right amount of rows, but the data is not displayed.

Once the first element is selected from the first dropdown list i.e. 'coffee name', the second list comes with the correct line number 3 but not with the correct data < P> Current code:

  Protected Zero empty dropdown list 1 selected index-based (object sender, eventAggas E) {string option = dropdownlist 1. Value selected; // If selected elements run 'Name of coffee' method. If (option == "coffee name") {bindCoffeeNames (); }} Private Zero Bind Coffee () {Query DB and tie data in second dropdown list. SqlConnection sqlcon = New SqlConnection (connstring); SqlCommand sqlcmd = New SqlCommand ("Coffee Ace by Choosing Coffee Number with Coffee Order", SQLX); SqlDataAdapter adp = new SqlDataAdapter (sqlcmd); Dataset ds = new dataset (); Adp.fill (ds); DropDownList2.DataSource = ds.Tables [0]; DropDownList2.DataBind (); }     

  1. Autopost will set fire on change only if you have an empty dropdown Want to include a default placeholder in the list. You can do this in code:

      dropdownlist1.Items.Instert (0, new list item ("Select an item", String.Empty)); Dropdown list 1. Selected index = 0;   

    Or in markup:

      & amp; Asp: dropdown list id = "ddlPersons" runat = "server" display and bounded items = "true" DataValueField = "ID" DataTextField = "name" & gt; & Lt; ASP: ListItem & gt; - Please select the person - & lt; / Asp: ListItem & gt; & Lt; / ASP: DropDownList & gt;    
  2. Add this line to bind coffee beanes:

    dropdown list 2. DataTiffField = "CoffeeName"; In addition, it is a good idea to specify a DataValueField (you can also return CoffeeId ) to To change your query, but this is not required)

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -