jueves, 31 de octubre de 2013

ASP - Inside a Gridview how get the DropDownList index

In the view:


  • 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:


  1. Cast the sender object to the element you wanted to know the index, for our case a DropDownList
  2. From the DropDownList get the GridViewRow through the parent property
  3. 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;
    }

No hay comentarios:

Publicar un comentario