- Define the method in the event OnSelectedIndexChanged
- Add the tag AutoPostBack="true"
<asp:TemplateField HeaderText="Competence Domain">
<ItemTemplate>
<asp:DropDownList ID="ddlCompDomain" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="ddlCompDomain_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
In the code inside the method ddlCompDomain_SelectedIndexChanged:
- Cast the sender object to the element you wanted to know the index, for our case a DropDownList
- From the DropDownList get the GridViewRow through the parent property
- Get the index with RowIndex
protected void ddlCompDomain_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlCurrentDropDownList = (DropDownList)sender;
GridViewRow grdrDropDownRow = ((GridViewRow)ddlCurrentDropDownList.Parent.Parent);
TextBox2.Text = grdrDropDownRow.RowIndex.ToString();
TextBox3.Text = ddlCurrentDropDownList.SelectedItem.Text;
}