Gridview embeded control reference parent object

There is a dropdownlist control in a gridview, see following is how to reference the gridview object when the OnSelectedIndexChanged even being triggered. This example also shows how to get the next row in a gridview.

aspx page:


                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlQuestionMLAName" runat="server" OnSelectedIndexChanged = "GdvwEdit_ddlQuestionMLAName_Change"  AutoPostBack ="true" />
                                    <asp:CheckBox ID="cbOutOfOrder" Text="Out&nbsp;of&nbsp;Order" runat="server" />
                                </ItemTemplate>

aspx.vb page

    Protected Sub GdvwEdit_ddlQuestionMLAName_Change(ByVal sender As System.Web.UI.WebControls.DropDownList, ByVal e As EventArgs)
        Dim grdRow As GridViewRow
        grdRow = CType(sender.Parent.Parent, GridViewRow)

        Dim gdvw As GridView
        gdvw = CType(grdRow.NamingContainer(), GridView)

        Dim secondrow As GridViewRow
        Dim thirdrow As GridViewRow

        If gdvw.DataKeys(grdRow.RowIndex).Values("QUESTIONTYPE").ToString.Trim = "000" Then
            secondrow = gdvw.Rows(grdRow.RowIndex + 1)
            CType(secondrow.FindControl("ddlQuestionMLAName"), DropDownList).SelectedIndex = sender.SelectedIndex
            CType(secondrow.FindControl("ddlQuestionMLAName"), DropDownList).SelectedValue = sender.SelectedValue


            thirdrow = gdvw.Rows(grdRow.RowIndex + 2)
            CType(thirdrow.FindControl("ddlQuestionMLAName"), DropDownList).SelectedIndex = sender.SelectedIndex
            CType(thirdrow.FindControl("ddlQuestionMLAName"), DropDownList).SelectedValue = sender.SelectedValue

        End If

    End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s