Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically loading a dropdown box 1

Status
Not open for further replies.

ceyhorn

Programmer
Nov 20, 2003
93
US
I have a page with two dropdown boxes and a datagrid. The first dropdown box has content already binded to it. Now, based on the selection of dropdown box 1, I want the second box to load some content based on the id of the selection from 1. And then I would like a list of items to appear in the datagrid based on the box 2 selection. I am storing data in sql server and have all of the items related by ID's. Any suggestions?

Thanks in advance,
Chris Eyhorn
 
set the Auto Postback properity of the check box to True

Then on the SelectedIndexChange event set the SelectedItem.Value of the combo box = to the parameter in your query(s) for the 2nd drop down list and datagrid.

Then bind the data to the 2nd combo box and the data grid.

Very similar to binding the data the to 1st dropdown list on page load. The main difference is the binding takes place within the combo box event, and not the page load.

Jason Meckley
Database Analyst
WITF
 
Hi Chris,

Firstly, set your first dropdownlist to autopostback to the server

<asp:dropdownlist id="ddl_One" runat="server" autopostback=true />

Then on the SelectedIndexChanged event of the first dropdownlist, run a query to bind the associated data to dropdownlist two.

ie: "Select * from tblTable1 where x = " + ddl_One.SelectedItem.Value.ToString()

I have this little method to bind data to a dropdownlist. You're free to use it.

Code:
private void Pop_DDL (string sqlQuery, DropDownList myList)
{
   //sqlQuery: select field1 = dropdown text, field2 = dropdown value
   SqlConnection objConn = new SqlConnection (connString);
   SqlCommand objComm = new SqlCommand (sqlQuery, objConn);

   objConn.Open ();
   SqlDataReader objDR = objComm.ExecuteReader ();

   while (objDR.Read ())
   {
      ListItem item = new ListItem (objDR[0].ToString(), objDR[1].ToString());
      myList.Items.Add (item);
   }
   objDR.Close ();
   objConn.Close ();
}

HTH,

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top