获取动态下拉列表选择的选定行索引

时间:2009-05-29 20:59:40

标签: asp.net vb.net visual-studio gridview drop-down-menu

我知道这个问题有点不稳定,可能会产生误导,但我在行上有一个带有下拉列表的gridview。我为SelectedIndexChanged创建了一个AddHandler和一个Delegate,它到达了sub。这是代码:

AddHandler ddlmgr.SelectedIndexChanged, AddressOf ddlmgr_SelectedIndexChanged
Public Delegate Sub DropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As DropDownList_SelectedIndexChanged)

Protected Sub ddlmgr_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)


End Sub

如果没有调用GridView_RowCommand,我如何获得行的ID?

4 个答案:

答案 0 :(得分:1)

你需要做一些小工作,因为我无法提供100%的细节,而无需自己编写代码并在我自己测试,我目前无法做到,但代码应该遵循以下几行

在ddlmgr_SelectedIndexChaged中,

  1. 将您的发件人投放到DropDownList
  2. 访问下拉列表的part属性。
  3. 检查它是一个GridItem(或者是repeateritem或者哪个,你明白了)
  4. 如果是,请获取项目itemindex。如果没有访问其父属性。
  5. 继续,直到找到Row对象。

答案 1 :(得分:1)

希望这会有所帮助。如果没有,也许有更多自由访问权限的人可以加入

DropDownList ddl = (DropDownList)sender;
Control p = ddl.Parent;

//you are going to loop because the immediate 
//parent may not be the repeater item but instead a 
//container control of some kind (say a template)
while (p.GetType() != typeof(RepeaterItem))
{
     p = p.Parent;
     if (p == null) return; //we have reached the top of the control tree
}
RepeaterItem ri = (RepeaterItem)p;
int index = ri.ItemIndex
return index;

答案 2 :(得分:0)

DropDownList ddltxt =(DropDownList)sender;         string temp2 = ddltxt.SelectedItem.Text;         string temp3 = ddltxt.SelectedItem.Value;         string temp = ddltxt.ID.ToString();         int strlength = temp.Length;         string strLastchar = temp.Substring(strlength - 1,1);         int intlastchar = int.Parse(strLastchar.ToString());         string commonpart = temp.Substring(0,strlength - 1);

    if (intlastchar == 1)
    {
        string targetdropdownid = commonpart + "2";
        DropDownList targetlist = (DropDownList)TableRow11.FindControl(targetdropdownid);
        using (conn = new SqlConnection(ConnectionString))

答案 3 :(得分:0)

很棒的工作 对我来说绝对没问题

DropDownList ddl = (DropDownList)sender;
Control p = ddl.Parent;
//you are going to loop because the immediate
//parent may not be the repeater item but instead a 
//container control of some kind (say a template)
while (p.GetType() != typeof(RepeaterItem))
{     
  p = p.Parent;     
  if (p == null) 
      return; //we have reached the top of the control tree
}
RepeaterItem ri = (RepeaterItem)p;
int index = ri.ItemIndexreturn index;