[ Listview DropDownList FindControl without Edit Button ]

I am having trouble populating a dropdownlist from a dataset when the dropdownlist is embedded in a listview. I get "Object reference not set to an instance of an object." because, I believe, I am not accessing the dropdownlist correctly using FindControl

We do NOT want edit buttons on this listview, we want every line to be editable at once. Most examples online have edit buttons. That might be part of my problem.

Most examples online also use a datasource defined in the ASPX page. We've been using exclusively Oracle.DataAccess sys_refcursor to populate datasets, and then use the dataset as the datasource. So I have to populate the drop down from the VB side, not but using a datasource in the aspx page.

I can independantly populate a listview, and independantly populate a dropdownlist, but walking and chewing gum is a failure.

So here are some snippets of what I got:

<asp:ListView ID="lvMFGRS" runat="server">
    <ItemTemplate>
        <tr runat="server">
        <td runat="server">
           <asp:Label ID="NameLabel" runat="server" Text='<%#Eval("NEW_MANUFACTURER")%>' />
        </td>
        <td runat="server">
            <asp:dropdownlist ID="ddMFGRS"   runat="server" />
        </td>
        </tr>
    </ItemTemplate>
</asp:ListView>

Then something like this on the vb side:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   newmfglist() 'just sticks data in the "NameLabel" and databinds, and this works fine.
   For Each lvItem In lvMFGRS.Items
      Dim ddlist As DropDownList = lvMFGRS.FindControl("ddMFGRS")
         For Each tbl2 As DataTable In dsmfgrlist.Tables
             For Each dr2 As DataRow In tbl2.Rows
                Dim str1 As String
                Dim str2 As String
                str1 = "dog" 'dr2.Field(Of String)("DataValueField")
                str2 = "cat" 'dr2.Field(Of String)("DataTextField")
                ddlist.Items.Add(New ListItem(str2, str1))  'this is where I get the error
             Next
          Next
       Next

End Sub

So what do you think I'm doing wrong...

Answer 1


The answer is:

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   newmfglist() 'just sticks data in the "NameLabel" and databinds, and this works fine.
   For Each lvItem In lvMFGRS.Items
      Dim ddlist As DropDownList = lvItem.FindControl("ddMFGRS")
         For Each tbl2 As DataTable In dsmfgrlist.Tables
             For Each dr2 As DataRow In tbl2.Rows
                Dim str1 As String
                Dim str2 As String
                str1 = "dog" 'dr2.Field(Of String)("DataValueField")
                str2 = "cat" 'dr2.Field(Of String)("DataTextField")
                ddlist.Items.Add(New ListItem(str2, str1))  'this is where I get the error
             Next
          Next
       Next