C# 如何在formview asp.net中将数据从dropdownlist绑定到另一个

C# 如何在formview asp.net中将数据从dropdownlist绑定到另一个,c#,asp.net,C#,Asp.net,我在formview InsertItemTemplate中有2个dropdownlist,我希望当我更改dropdownlist A中的selectedindex时,dropdownlist B将更改。 我在dropdownlist A中更改selectedindex时出错:“诸如Eval()、XPath()和Bind()等数据绑定方法只能在数据绑定控件的上下文中使用。” 我在这里被困了一个小时,谁都知道 下面是我的设计代码: <asp:Label ID="lblClass" runat

我在formview InsertItemTemplate中有2个dropdownlist,我希望当我更改dropdownlist A中的selectedindex时,dropdownlist B将更改。 我在dropdownlist A中更改selectedindex时出错:“诸如Eval()、XPath()和Bind()等数据绑定方法只能在数据绑定控件的上下文中使用。”

我在这里被困了一个小时,谁都知道

下面是我的设计代码:

<asp:Label ID="lblClass" runat="server" Text="Class:"></asp:Label>
<asp:DropDownList ID="cboClass" runat="server" AutoPostBack="true" Width="100%" DataSourceID="sdsClass" DataTextField="ClassName" DataValueField="ClassID" OnSelectedIndexChanged="cboClass_SelectedIndexChanged"></asp:DropDownList>
<asp:Label ID="lblStudent" runat="server" Text="Student Name:"></asp:Label>
<asp:DropDownList ID="cboStudent" runat="server" SelectedValue='<%#Bind("StudentID") %>' Width="100%" DataSourceID="sdsStudent" DataTextField="StudentName" DataValueField="StudentID"></asp:DropDownList>

下拉列表不是
数据绑定控件
,因此不能对其使用
Bind()
方法。当您将下拉列表绑定到数据源时,您可以设置下拉列表的
SelectedValue
属性。您有办法处理它吗?当您将下拉列表绑定到数据源时,您需要设置下拉列表的SelectedValue属性。你就是这样处理的。您能否共享将
cboStudent
绑定到数据源的代码。您是如何为
StudentID
赋值的?@ChetanRanpariya我在formview中使用InsertItemTemplate插入,cboStudent正在用cboClass中选择的值绑定数据,这里是我的cboStudent的SqlDataSource:@tgolisch i do,我在更改所选cboClass时尝试更改cboStudent数据时出错,如果我不这样做,它工作正常下拉列表不是一个
数据绑定控件
,因此您不能对其使用
Bind()
方法。当您将下拉列表绑定到数据源时,您可以设置下拉列表的
SelectedValue
属性。您有办法处理它吗?当您将下拉列表绑定到数据源时,您需要设置下拉列表的SelectedValue属性。你就是这样处理的。您能否共享将
cboStudent
绑定到数据源的代码。您是如何为
StudentID
赋值的?@ChetanRanpariya我在formview中使用InsertItemTemplate插入,cboStudent正在用cboClass中选择的值绑定数据,这里是我的cboStudent的SqlDataSource:@tgolisch i do,我在更改所选cboClass时尝试更改cboStudent数据时出错,如果我不这样做,它的工作很好
protected void cboClass_SelectedIndexChanged(object sender, EventArgs e)
{
    sdsStudent.SelectParameters["ClassID"].DefaultValue = (sender as DropDownList).SelectedValue;
    (frmExamScore.FindControl("cboStudent") as DropDownList).DataBind();
}