locked
Can't Find DropDownList in InsertItemTemplate of ListView RRS feed

  • Question

  • User646364117 posted

    I am not using a master page. I thought the problem might have to do with jquery tabs, naming containers, etc so I stripped out everything but still have the problem.

    Object reference not set to an instance of an object.

    at this line:

    Dim ddl As DropDownList = CType(LvTemplate.InsertItem.FindControl("ddlCategory"), DropDownList)

        Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    
            If Not Page.IsPostBack Then
                FillCategories()
    
    
            End If
        End Sub
        Protected Sub FillCategories()
    
            Dim sqlConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DB_A54278_steve48ConnectionString").ConnectionString)
    
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.CommandText = "SELECT category.id AS CatID,category.name AS CatName
    	                                    FROM category
    	                                    ORDER BY category.name"
            cmd.CommandType = CommandType.Text
            cmd.Connection = sqlConn
            sqlConn.Open()
            Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
            Dim dt As DataTable = New DataTable()
            da.Fill(dt)
    
            Dim ddl As DropDownList = CType(LvTemplate.InsertItem.FindControl("ddlCategory"), DropDownList)
            ddl.DataSource = dt
            ddl.DataTextField = "CatName"
            ddl.DataValueField = "CatID"
            ddl.DataBind()
            ddl.Items.Insert(0, New ListItem("--Select Category--", ""))
            sqlConn.Close()
    
        End Sub
    
    
    End Class

    My markup is

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="test2notabs.aspx.vb" Inherits="test2notabs" ValidateRequest="false" %>
    
    
    
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <body>
    
        <form id="form1" runat="server">
    
    
            <asp:ListView ID="LvTemplate" runat="server" InsertItemPosition="LastItem">
                                
                                        <InsertItemTemplate>
                                            <tr style="">
    
                                                <td style="border: thin solid #C0C0C0;border-collapse:collapse;">&nbsp;</td>
                                                <td style="border: thin solid #C0C0C0;border-collapse:collapse;">
                                                    <asp:Label runat="server" ID="lblNameInsert" AssociatedControlID="txtTemplateName2" Text="name: " />
                                                    <asp:TextBox ID="txtTemplateName2" runat="server" Text='<%# Bind("name") %>' Style="width: 75%;" />
                                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                                                        ErrorMessage="name is required"
                                                        ControlToValidate="txtTemplateName2"
                                                        ValidationGroup="myLvTemplateInsert" />
                                                </td>
                                                <td>
                                                    <asp:DropDownList ID="ddlCategory" runat="server" >
                                                    </asp:DropDownList>
                                                     <asp:RequiredFieldValidator ID="rfv_ddlCategory" runat="server" ForeColor="Red"
                                                        Font-Bold="true" Font-Size="18px" ToolTip="Select Category." ErrorMessage="*"
                                                        ControlToValidate="ddlCategory" ValidationGroup="myLvTemplateInsert" Display="Dynamic"
                                                        InitialValue="-1">
                                                     </asp:RequiredFieldValidator>
                                                </td>
                                                <td style="border: thin solid #C0C0C0;border-collapse:collapse;">
                                                    <asp:ImageButton ID="ImageButton1" runat="server" src="images/addnew_21.png" CommandName="Insert"
                                                        CausesValidation="true" ValidationGroup="myLvTemplateInsert" />
    
                                                    <asp:ImageButton ID="ImageButton2" runat="server" src="images/cancel_20.png" CommandName="Cancel" />
    
                                                </td>
                                            </tr>
                                        </InsertItemTemplate>
                                        <ItemTemplate>
                                            <div >
                                                <tr runat="server">
                                                    
                                                    <td  style="border: thin solid #C0C0C0;border-collapse:collapse;" >
                                                        <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' Visible="false" />
                                                    </td>
                                                    <td style="border: thin solid #C0C0C0;border-collapse:collapse;" >
                                                        <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
                                                    </td>
                                                    <td style="border: thin solid #C0C0C0;border-collapse:collapse;" >
                                                        
                                                    </td>
                                                    <td style="border: thin solid #C0C0C0;border-collapse:collapse;">
                                                        <asp:ImageButton ID="btnEdit" runat="server" src="images/edit_20.png" CommandName="Edit" />
                                                        <asp:ImageButton ID="btnDelete" runat="server" src="images/delete_20.png" CommandName="Delete" />
                                                    </td>
    
                                                </tr>
                                            </div>
                                        </ItemTemplate>
                                        <LayoutTemplate>
    
                                            <table  runat="server" id="Categories" cellpadding="2" cellspacing="0">
                                                <tr runat="server">
                                                    <th runat="server" style="border: thin solid #C0C0C0;border-collapse:collapse;">Template Name</th>
                                                    <th runat="server" style="border: thin solid #C0C0C0;border-collapse:collapse;">Category</th>
                                                    <th runat="server" style="border: thin solid #C0C0C0;border-collapse:collapse;">Edit/Delete</th>
                                                </tr>
                                                <tr id="itemPlaceholder" runat="server">
                                                </tr>
                                            </table>
    
                                        </LayoutTemplate>
                                    </asp:ListView>
    
    
        </form>
    
    
    
    </body>

    Saturday, January 25, 2020 2:13 PM

All replies

  • User475983607 posted

    sg48asp

    I am not using a master page. I thought the problem might have to do with jquery tabs, naming containers, etc so I stripped out everything but still have the problem.

    The ddlCategory server control does not exist in the ItemTemplate.  Only one template is shown at a time.  The standard programming pattern is populating the template items when the template is active usually in an event.  I believe you'll find the ListView reference documentation helpful.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listview?view=netframework-4.8

    Saturday, January 25, 2020 2:31 PM
  • User665608656 posted

    Hi sg48asp,

    This problem occurs because you did not bind a data source to the listview.

    After you bind the data sources, the FillCategories method will run successfully.

    You can first bind the datatable containing the id and name fields to the listview, regardless of whether there is any data in it, you should bind it so that you can successfully obtain the controls in the InsertItem.

    Here is an example:

    Class SurroundingClass
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            If Not Page.IsPostBack Then
                BindList() 'bind list view first
                FillCategories() ' then bind the dropdownlist
            End If
        End Sub
    
        Protected Sub BindList()
            Dim sqlConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DB_A54278_steve48ConnectionString").ConnectionString)
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.CommandText = "SELECT id,name FROM table1"
            cmd.CommandType = CommandType.Text
            cmd.Connection = sqlConn
            sqlConn.Open()
            Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
            Dim dt As DataTable = New DataTable()
            da.Fill(dt)
            LvTemplate.DataSource = dt
            LvTemplate.DataBind()
            sqlConn.Close()
        End Sub
    
        Protected Sub FillCategories()
            Dim sqlConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("DB_A54278_steve48ConnectionString").ConnectionString)
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.CommandText = "SELECT category.id AS CatID,category.name AS CatName
    FROM category
    ORDER BY category.name" cmd.CommandType = CommandType.Text cmd.Connection = sqlConn sqlConn.Open() Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) Dim ddl As DropDownList = CType(LvTemplate.InsertItem.FindControl("ddlCategory"), DropDownList) ddl.DataSource = dt ddl.DataTextField = "CatName" ddl.DataValueField = "CatID" ddl.DataBind() ddl.Items.Insert(0, New ListItem("--Select Category--", "")) sqlConn.Close() End Sub End Class

    Here is the result :

    Best Regards,

    YongQing.

    Monday, January 27, 2020 5:54 AM