Reputation: 997
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.
Upvotes: 7
Views: 27283
Reputation: 11
Protected Sub ddlneedlocationcmf_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim gvrow As GridViewRow = CType(sender, DropDownList).NamingContainer
Dim rowindex As Integer = CType(gvrow, GridViewRow).RowIndex
End Sub
Upvotes: 1
Reputation: 460108
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.
DropDownList ddl = (DropDownList)sender;
If you also need to get a reference to the GridViewRow
of the DropDownList
(or any other control in a TemplateField of a GridView), you can use the NamingContainer
property.
GridViewRow row = (GridViewRow)ddl.NamingContainer;
but I need to get the row index for getting a value from a templatefield who's not a dropdown is a textbox
You can get any control once you have the GridViewRow
reference by using row.FindControl("ID")
(TemplateField) or row.Cells[index].Controls[0]
(BoundField).
For example (assuming there's a TextBox
in another column):
TextBox txtName = (TextBox)row.FindControl("TxtName");
Upvotes: 21
Reputation: 56769
If all you are looking for is the value of the dropdownlist, that's passed in as the sender
:
DropDownList ddl = sender as DropDownList;
if (ddl.SelectedValue == "1")
// do something...
Upvotes: 4