Cannot access a dropdownlist inside the gridview

wsyeager

Centurion
Joined
Apr 10, 2003
Messages
140
Location
Weston, FL
I am using the Gridview control without a DataSource control.

When I click on the Edit button, the page posts back and enters the following code:

Visual Basic:
    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing

        Try
            Dim row As GridViewRow = GridView1.Rows(e.NewEditIndex)
            [B]Dim ddlCustomerID As DropDownList = row.FindControl("ddlCustomerID")[/B]            ddlCustomerID.DataSource = dstCustomers
            ddlCustomerID.SelectedIndex = ddlCustomerID.Items.IndexOf(ddlCustomerID.Items.FindByValue(dstCustomers.Tables(0).Rows(row.RowIndex).Item("CustomerID")))
            ddlCustomerID.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

    End Sub

I cannot get an instance of the control in the above boldfaced code after passing thru that line during debugging. It has a value of Nothing.

My html code is set up as follows (shortened for brevity) within the Gridview control:

Code:
<asp:TemplateField HeaderText="CustomerID">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlCustomerID" runat="server" Text='<%# Bind("CustomerID") %>' DataValueField="CustomerID" DataTextField="ContactName"></asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblCustomerID" runat="server" Text='<%# Bind("CustomerID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

As you can see above, I have the name of the control defined in the above code in the EditItemTemplate. I am assuming that during the RowEditing event, I should be able to access the control in order to make it into a dropdownlist. During the RowUpdating event which is fired immediately after the RowEditing, I will get the SelectedValue. During non-editing, it is simply a Label control and grabs the value in the database for the CustomerID. It is only when I want to edit the row, that I cannot get an instance of the control.

Can someone help me out here please? How can I get an instance of the control?
 
Last edited by a moderator:
Back
Top