get selected row index of dynamic dropdown list se

2019-08-05 17:41发布

问题:

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?

回答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.

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.


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

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