且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在选定索引更改时查找下拉列表选择值

更新时间:2022-12-16 14:58:36

您没有在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 a
DataList contains many copies of each control that is in a template.You need to iterate all items in DataList 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.