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

时间:2023-01-27 13:32:30

I know the question is a little choppy and perhaps misleading,but I have a gridview with dropdownlists on the rows. I created an AddHandler and a Delegate for the SelectedIndexChanged and it gets to the sub. Here is the code for that:

我知道这个问题有点不稳定,也许会产生误导,但我在行上有一个带有下拉列表的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

How can i get the row's Id if GridView_RowCommand is not called?

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

4 个解决方案

#1


Great work Works absolutely fine for me

伟大的工作对我来说绝对没问题

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;

#2


You will need to do a bit of the legwork as I cant provide 100% specifics without writing out the code and testing it on my own here, which I am unable to do at present, but the code should go along these lines.

你需要做一些腿部工作,因为我无法提供100%的细节而没有在我自己的地方写出代码和测试,我目前无法做到,但代码应该遵循这些方针。

within the ddlmgr_SelectedIndexChaged,

在ddlmgr_SelectedIndexChaged中,

  1. cast your sender to a DropDownList
  2. 将您的发件人转发到DropDownList

  3. access the part property of the dropdownlist.
  4. 访问下拉列表的part属性。

  5. Check it is is a GridItem (or repeateritem or whichever, you get the idea)
  6. 检查它是一个GridItem(或repeateritem或其中任何一个,你明白了)

  7. If so, get the items itemindex. If not access its parent property.
  8. 如果是这样,请获取项目itemindex。如果不访问其父属性。

  9. Continue until you find your Row object.
  10. 继续,直到找到Row对象。

#3


Hopefully this helps. If not, perhaps someone with a bit more liberal access can chime in

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

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;

#4


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);

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))

#1


Great work Works absolutely fine for me

伟大的工作对我来说绝对没问题

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;

#2


You will need to do a bit of the legwork as I cant provide 100% specifics without writing out the code and testing it on my own here, which I am unable to do at present, but the code should go along these lines.

你需要做一些腿部工作,因为我无法提供100%的细节而没有在我自己的地方写出代码和测试,我目前无法做到,但代码应该遵循这些方针。

within the ddlmgr_SelectedIndexChaged,

在ddlmgr_SelectedIndexChaged中,

  1. cast your sender to a DropDownList
  2. 将您的发件人转发到DropDownList

  3. access the part property of the dropdownlist.
  4. 访问下拉列表的part属性。

  5. Check it is is a GridItem (or repeateritem or whichever, you get the idea)
  6. 检查它是一个GridItem(或repeateritem或其中任何一个,你明白了)

  7. If so, get the items itemindex. If not access its parent property.
  8. 如果是这样,请获取项目itemindex。如果不访问其父属性。

  9. Continue until you find your Row object.
  10. 继续,直到找到Row对象。

#3


Hopefully this helps. If not, perhaps someone with a bit more liberal access can chime in

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

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;

#4


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);

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))