locked
Issue Creating Custom ListView LayoutTemplate RRS feed

  • Question

  • User1342168525 posted

    Hello,

    I am working on a project where I will be displaying a set of data, however, depending on the user's settings, the columns that will be displayed will vary greatly. I am using a ListView control because of its flexible template system. I will need to define the template at run-time, since I will never know what columns need to be created and displayed until then.

    So I decided to create my own LayoutTemplate and ItemTemplate classes. I am running into an issue with the LayoutTemplate (have not tried the ItemTemplate yet).

    When I run my code, I get the following error:

    An item placeholder must be specified on ListView ''. Specify an item placeholder by setting a control's ID property to "itemPlaceholder". The item placeholder control must also specify runat="server".

    Here is the code for my LayoutTemplate class:

    Public Class v_LayoutTemplate
        Implements ITemplate

        Private _strcolconfig As String

        Public Sub New(ByVal colconfig As String)
            _strcolconfig = colconfig
        End Sub

        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
            'create a new HTML table object and set its attributes...
            Dim t As New HtmlGenericControl("table")
            t.Attributes.Add("class", "j-table")
            t.Attributes.Add("cellpadding", "3")
            t.Attributes.Add("cellspacing", "0")

            'add a table header to the table...
            Dim thead As New HtmlGenericControl("thead")

            'add the new table row objects...
            Dim tr As New HtmlGenericControl("tr")
            tr.Attributes.Add("class", "rpt_header_row")

            'create the initial columns used for icons...
            Dim th As New HtmlGenericControl("th")
            th.Attributes.Add("class", "ignore-drop")
            th.Attributes.Add("style", "border-left: solid 1px #275d86;")
            th.InnerHtml = " "
            tr.Controls.Add(th)

            th = New HtmlGenericControl("th")
            th.InnerHtml = " "
            tr.Controls.Add(th)

            'split the configuration string to get the list of columns we have to create...
            Dim arrCol As String() = _strcolconfig.Split("|")

            'create each of the content column headers...
            For Each s As String In arrCol
                th = New HtmlGenericControl("th")
                th.InnerHtml = "<divdrag"">" & s & "</div>"
                tr.Controls.Add(th)
            Next

            'add the closing column used for drag and drop purposes...
            th = New HtmlGenericControl("th")
            th.Attributes.Add("class", "ignore-drop")
            th.Attributes.Add("style", "border-right: solid 1px #275d86;")
            th.InnerHtml = "&nbsp;"
            tr.Controls.Add(th)

            thead.Controls.Add(tr)

            t.Controls.Add(thead)

            'create the tbody object...
            Dim tbody As New HtmlGenericControl("tbody")

            'Dim trph As New HtmlGenericControl("tr")
            'trph.Attributes.Add("runat", "server")
            'trph.Attributes.Add("id", "itemPlaceholder")

            Dim ph As New PlaceHolder()
            ph.ID = "itemPlaceholder"

            tbody.Controls.Add(ph)
            t.Controls.Add(tbody)
        End Sub
    End Class

    At runtime, I already have the ListView control on my webform and have simply left off the LayoutTemplate portion. (I know this is not the proper way, but I am only doing this for testing to see if I can get this to work)

    Private Sub BindData()
            lvReport.LayoutTemplate = New v_LayoutTemplate("File No.|Cust No.|Opened|Entry No.|Concept|SCAC|Desc. Of Goods|ETA POE|ETA POI|ETA Dest.")

            Dim cmd As New SqlCommand("test_ReturnData", conn)
            cmd.CommandType = CommandType.StoredProcedure
            conn.Open()
            Dim rd As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

            With lvReport
                .DataSource = rd
                .DataBind()
            End With
            rd.Close()
        End Sub

    I can't seem to find a lot of detailed on this info out on the net. Any help would be greatly appreciated.


    Thanks!


    Bob Gibilaro

    Friday, November 20, 2009 1:19 PM

Answers

  • User-1635004338 posted

    Hi bgibilaro,

    You can add <asp:Table> control as server control into the container of template directly. Then set the ID of Table to "itemPlaceholder", which solves the issue. Then you can add TableRow and TableCell to this Table control. The key point is you have to add an container control, its ID should be "itemPlaceholder".

    Thanks,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 25, 2009 1:18 AM