Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I haven't tested but sure this is what you are looking for:</p> <p>CODE BEHIND CHANGES:</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { //Bind data to first drop down box on pageload DropDownList1.DataTextField = "ItemName";//field returned from db DropDownList1.DataValueField = "ItemID";//field returned from db //binds to data returned from db - first items have no parent id's DropDownList1.DataSource = GetItems(0); DropDownList1.DataBind(); DropDownList1.Items.Insert(0, new ListItem("All Borgs", Session["BorgID"].ToString())); DropDownList1.Items[0].Selected = true; } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { //When value is selected in first drop down populate second drop down using the id selected in first drop down DropDownList2.DataTextField = "ItemName";//field returned from db DropDownList2.DataValueField = "ItemID";//field returned from db //binds to data returned from db DropDownList2.DataSource = GetItems(Convert.ToInt32(DropDownList1.DataValueField)); DropDownList2.DataBind(); DropDownList2.Items.Insert(0, new ListItem("[Choose]", "-1")); DropDownList2.Items[0].Selected = true; } protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { //When value is selected in second drop down populate third drop down using the id selected in second drop down DropDownList3.DataTextField = "ItemName";//field returned from db DropDownList3.DataValueField = "ItemID";//field returned from db //binds to data returned from db DropDownList3.DataSource = GetItems(Convert.ToInt32(DropDownList2.DataValueField)); DropDownList3.DataBind(); DropDownList3.Items.Insert(0, new ListItem("[Choose]", "-1")); DropDownList3.Items[0].Selected = true; } public DataTable GetItems(int _ParentItemID) { SqlConnection sqlconn = null; sqlconn = new SqlConnection(); //SQLConnectionstring declared in web.config to connect to db sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["SQLConnectionstring"].ConnectionString; //select data from database SqlCommand sqlcommand = new SqlCommand("SELECT ItemName, ItemID FROM YourTable WHERE ParentItemID = " + _ParentItemID, sqlconn); sqlcommand.CommandType = System.Data.CommandType.Text; SqlDataAdapter adapter = new SqlDataAdapter(sqlcommand); //create and fill datatable to return results to call and bind DataTable dt = new DataTable(); adapter.Fill(dt); sqlconn.Dispose(); sqlconn.Close(); return dt; } </code></pre> <p>FRONT END CHANGES:</p> <pre><code>&lt;asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged"&gt; &lt;/asp:DropDownList&gt; &lt;br /&gt; &lt;asp:DropDownList ID="DropDownList2" runat="server" onselectedindexchanged="DropDownList2_SelectedIndexChanged"&gt; &lt;/asp:DropDownList&gt; &lt;br /&gt; &lt;asp:DropDownList ID="DropDownList3" runat="server"&gt; &lt;/asp:DropDownList&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload