Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于DataList-c中的另一个下拉列表SelectedValue绑定一个下拉列表_C#_Asp.net_Drop Down Menu_Datalist - Fatal编程技术网

C# 基于DataList-c中的另一个下拉列表SelectedValue绑定一个下拉列表

C# 基于DataList-c中的另一个下拉列表SelectedValue绑定一个下拉列表,c#,asp.net,drop-down-menu,datalist,C#,Asp.net,Drop Down Menu,Datalist,我在数据列表中使用了3个DropDownList。因此,每行包含3个DropDownList。DataList还包含DataKeyField。 现在,如果用户从DropDownList1选择值,那么我想绑定DropDownList2,如果用户从DropDownList2选择值,那么我想绑定DropDownList3。我正在使用SelectedIndexChanged,我能够获得相关DropDownList选定值的值。但是,若用户选择DropDownList2,那个么我还需要DropDownLis

我在数据列表中使用了3个DropDownList。因此,每行包含3个DropDownList。DataList还包含DataKeyField。 现在,如果用户从DropDownList1选择值,那么我想绑定DropDownList2,如果用户从DropDownList2选择值,那么我想绑定DropDownList3。我正在使用SelectedIndexChanged,我能够获得相关DropDownList选定值的值。但是,若用户选择DropDownList2,那个么我还需要DropDownList1的值,还需要受尊重的DataKeyField的值。 如何得到这个??? 我的代码示例:


我想你就快到了。你只需要这样。使用您的代码:

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{

    var DropDownList1 = (DropDownList)sender;
    var DropDownList2 = (DropDownList)sender;
    var DropDownList3 = (DropDownList)sender;

    string DDL1 = ((DropDownList)DropDownList1.NamingContainer.FindControl("DropDownList1")).SelectedValue;
    string DDL2 = ((DropDownList)DropDownList2.NamingContainer.FindControl("DropDownList2")).SelectedValue;
    string DDL3 = ((DropDownList)DropDownList3.NamingContainer.FindControl("DropDownList3")).SelectedValue;
}
无论哪种方式,我都会做一些不同的事情:

    item valueddl1 = DropDownList1.SelectedValue;

等等;它应该像weel一样工作,而且更简单。

如果我没有弄错的话,您的代码就在那里。代替.FindControlDropDownList3,只需对DropDownList1和DropDownList2执行一个查找控件。为同一对象创建三个变量并将它们命名为标记中实际上不同的对象是不必要的,也是令人困惑的。我不建议这样做。此外,OP询问如何获取DataKeyField值,因此您也应该在回答中包含该值。
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{

    var DropDownList1 = (DropDownList)sender;
    var DropDownList2 = (DropDownList)sender;
    var DropDownList3 = (DropDownList)sender;

    string DDL1 = ((DropDownList)DropDownList1.NamingContainer.FindControl("DropDownList1")).SelectedValue;
    string DDL2 = ((DropDownList)DropDownList2.NamingContainer.FindControl("DropDownList2")).SelectedValue;
    string DDL3 = ((DropDownList)DropDownList3.NamingContainer.FindControl("DropDownList3")).SelectedValue;
}
    item valueddl1 = DropDownList1.SelectedValue;