简体   繁体   English

根据另一个下拉列表ID填充下拉列表时出错

[英]error populating dropdown list based on another dropdown list ID

I have two dropdown lists and the second is populated based on the selected value of the first. 我有两个下拉列表,第二个基于第一个的选定值进行填充。 This is how i had my second dropdown list populated but it wasn't filling based on the selected item in the first which lead me to change how i am filling it. 这就是我填充第二个下拉列表的方式,但是它不是基于第一个中的所选项目填充的,这导致我更改了我的填充方式。

Before: 之前:

<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>

After: 后:

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

Child Method: 子方法:

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();
    }

Error: 错误:

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

尝试设置

DataMember as what you want

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM