2013-07-29 137 views
0

我有两个下拉列表,第二个是基于第一个选定的值填充。这是我如何填充我的第二个下拉列表,但它没有填充根据第一个选定的项目,这导致我改变我如何填充它。基于另一个下拉列表错误填充下拉列表ID

前:

<asp:DropDownList ID="RootDropDown" runat="server" AutoPostBack="True" DataSourceID="FirstChild" DataTextField="DisplayName" DataValueField="ID"></asp:DropDownList> 
     <asp:SqlDataSource ID="FirstChild" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" SelectCommand="SELECT e.DisplayName, e.ID , e.GUID 
FROM SomeTable e 
INNER JOIN TabletoTableMap em 
ON e.ID = em.OtherTableID 
WHERE em.SomeTableID = 9"+ ></asp:SqlDataSource> 

后:

<asp:DropDownList ID="RootDropDown" runat="server" AutoPostBack="True" DataSourceID="FirstChild" DataTextField="DisplayName" DataValueField="ID" OnSelectedIndexChanged="Child"></asp:DropDownList> 

儿童法:

protected void Child(object sender, EventArgs e) 
    { 
     String strConnection = "Data Source=.;Initial Catalog=ALE;Integrated Security=True"; 
     int RootID = Convert.ToInt32(CourseDropDown.SelectedValue); 
     System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strConnection); 
     con.Open(); 
     System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT e.DisplayName, e.ID , e.GUID FROM SomeTable e INNER JOIN TabletoTableMap em ON e.ID = em.OtherTableID WHERE em.SomeTableID="+RootID, con); 
     System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); 
     System.Data.DataSet ds = new System.Data.DataSet(); 
     da.Fill(ds); 
     con.Close(); 
     RootElementDropDown.DataSourceID = "FirstChild"; 
     RootElementDropDown.DataTextField = "DisplayName"; 
     RootElementDropDown.DataValueField = "ID"; 
     RootElementDropDown.DataBind(); 
    } 

错误:

The DataSourceID of 'RootDropDown' must be the ID of a control of type IDataSource. A control with ID 'FirstChild' could not be found. 
+0

请接受答案,,,, !!!! – Aravind

回答

0

试穿套装

DataMember as what you want 
+0

你的意思是什么? – Masriyah

+0

您已经设置了DataValuefield ID ..当第一个dropdownlist的selectedindexchanged添加一个方法来填充第二个下拉列表时,根据第一个选择先设置一个datamember属性,以便您了解哪个项目被选中以及要填充什么 – Aravind

相关问题