I have a asp.net gridview. A template field of type dropdownlist is contained in a column:
<asp:TemplateField HeaderTextLabel="strManagedOETeamart">
<ItemTemplate>
<asp:DropDownList runat="server" AutoPostBack="True" OnSelectedIndexChanged="SelectedTeamartChanged">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Silver"> Silver </asp:ListItem>
<asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
<asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
<asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
How can I get the GridViewRow when the user selects another item?
protected void SelectedTeamartChanged(object sender, EventArgs e)
{
DropDownList dropDown = (DropDownList) sender;
//I would like to know the GridViewRow this DropDownList is in
}
You’re looking for the
NamingContainerproperty:By the way, that works for any kind of web databound control like
GridView,DataList,RepeaterorListView.Consider this more complex requirement: you have a
GridViewwhich is nested in anotherGridView. Now you’re handling a DropDownList’sSelectedIndexChangedevent that is inside the child’sGridViewand you want to get the reference to theGridViewRowof the parentGridView:That’s the safest and easiest way to get it.