russds
russds

Reputation: 875

Dynamically change the number of header cells in listview LayoutTemplate

I'm trying to dynamically create a listview. on reports.aspx user selects a bunch of checkboxes. On the next page, user sees reports.aspx, and should see a table with columns of the checkboxes he selected. My idea was to create a listview, then dynamically change the header row of the LayoutTemplate, then change the select statement depending on which columns selected. This is what I have:

<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
<LayoutTemplate runat="server">
 <table  runat="server">
    <tr runat="server">
    <%
    ' Doesn't work because code blocks (<%%>) aren's allowed inside <LayoutTemplate> blocks
    'For Each i As String In Request.Form
                'Response.Write("<th>" & Request.Form(i) & "</th>")
    'Next
     %>
    </tr>
 </table>
   <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</LayoutTemplate>
...

Problem is that this doesn't work because i can't put a code block (<%%>) inside the LayoutTemplate. Is there a way in the code behind to edit the LayoutTemplate, or another way to cycle through the Request.Form vars and build the table header row with it?

Thanks for any advice/direction! -Russ

Upvotes: 0

Views: 3598

Answers (3)

russds
russds

Reputation: 875

I just created a separate table on the page, outside of the listview, that was the simple way of doing it.

 <asp:Table ID="HeaderTable" runat="server">
      <asp:TableHeaderRow ID="HeaderTableHeaderRow" runat="server" />
 </asp:Table>

 <asp:ListView ...>
     ...
 </asp:ListView>

Then in the code behind:

   For Each i As String In Request.Form
        If i.IndexOf("checkbox_") = 0 Then
            Dim c As New TableHeaderCell()
            Dim l As New LinkButton()
            l.Text = i.Substring(Len("checkbox_"))
            c.Controls.Add(l)
            c.CssClass = "customreport"
            HeaderTableHeaderRow.Cells.Add(c)
        End If
    Next

Pretty simple. So I didn't have to use a LayoutTemplate at all.

Upvotes: 0

rkw
rkw

Reputation: 7297

Since the control is already a server side control, try giving the and id and then modifying the header on pre-render:

<asp:ListView runat="server" ID="ReportListView" DataSourceID="ReportListViewSDS">
<LayoutTemplate runat="server">
 <table runat="server">
    <tr id='trCustomHeader" runat="server">

Then in your code behind, attach this logic to the listview's pre-render

ReportListView_PreRender(...)
{
    TableRow tr = ReportListView.FindControl("trCustomerHeader");
    TableCell tempCell = new TableCell();
    tempCell.Text = ...
    tr.Cells.Add(tempCell);
}

Upvotes: 1

James Johnson
James Johnson

Reputation: 46047

Try using the ItemTemplate for the binding syntax instead of the layout template. I believe the layout template is strictly for layout.

Also, it looks like you're using classic ASP code blocks. ASP.NET code blocks look like this:

For data binding:

<%# Eval("<COLUMN NAME>")%>

For other cases not involving data binding:

<%= Request.QueryString["Hello"] %>

Upvotes: 1

Related Questions