I’m using a gridview and sqldatasource.
I have a dropdownlist in my gridview with 2 values : Yes and No .
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
DropDownList ddl = ((DropDownList)row.FindControl("DropdownList1"));
if(ddl.selectedvalue == "1")
//etc..
}
I need to get the Row index because this GridViewRow row = GridView1.Rows[e.RowIndex]; is not available in the current event.
As @mellamokb has already mentioned, you always get the control that raised an event by the sender argument, you only have to cast it accordingly.
If you also need to get a reference to the
GridViewRowof theDropDownList(or any other control in a TemplateField of a GridView), you can use theNamingContainerproperty.You can get any control once you have the
GridViewRowreference by usingrow.FindControl("ID")(TemplateField) orrow.Cells[index].Controls[0](BoundField).For example (assuming there’s a
TextBoxin another column):