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:

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

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

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s