get selected row index of dynamic dropdown list se

2019-08-05 17:58发布

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:

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?

4条回答
别忘想泡老子
2楼-- · 2019-08-05 18:33

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;
查看更多
Summer. ? 凉城
3楼-- · 2019-08-05 18:42

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楼-- · 2019-08-05 18:43

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.

within the ddlmgr_SelectedIndexChaged,

  1. cast your sender to a DropDownList
  2. access the part property of the dropdownlist.
  3. Check it is is a GridItem (or repeateritem or whichever, you get the idea)
  4. If so, get the items itemindex. If not access its parent property.
  5. Continue until you find your Row object.
查看更多
forever°为你锁心
5楼-- · 2019-08-05 18:43

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))
查看更多
登录 后发表回答