如何在选定索引更改时查找下拉列表选择值
问题描述:
大家好,
我有一个数据列表.里面有两个下拉列表.在第一个下拉列表的选定索引上,我想绑定第二个下拉列表.为此,我需要选择第一个下拉列表的选定值.如何实现,因为我没有得到第一个下拉列表的选定值.
选定索引处的代码
Hello all,
I have a datalist.Inside it has two dropdowns. On the selected index of first dropdown I want to bind the second drop down.for that i have to need the selected value of first drop down.How it will be done bcz i am not getting the selected value of first drop down.
code at selected index
DropDownList ddlcountry= (DropDownList)DataList1.FindControl("ddlcountry");
int id=convert.toint32(ddlcountry.selectedvalue);
但在ddlcountry
处显示null
.
谢谢&问候,
amit
but it shows null
at ddlcountry
.
Thanks & Regards,
amit
答
您没有在DataList本身上找到控件.您可以在DataList的每个DataListItem上执行此操作.请参考下面的代码:
You dont find the control on the DataList itself. You do it on every DataListItem of the DataList. Refer to the code below:
(DropDownList)DataList1.Items[0].FindControl("ddlcountry");
FindControl
需要知道它正在查找哪个项目,因为DataLis
t包含模板中每个控件的许多副本.您需要对DataLis
t中的所有项目进行迭代以找到下拉菜单并对其进行操作,您可以使用下面的代码,
FindControl
needs to know which Item it is looking in because aDataLis
t contains many copies of each control that is in a template.You need to iterate all items inDataLis
t to find the dropdown and manipulate them, you can use the code below,
foreach(DataListItem dlistItem in DataList1.Items)
{
DropDownList ddlcountry= dlistItem.item[i].
findControl("ddlcountry") as DropDownList;
if(ddlcountry!=null)
{
int id=Convert.ToInt32(ddlcountry.SelectedValue);
}
}
希望这会有所帮助.
Hope this will help.