I need to hide a column in an asp:repeater
. Preferably, hide them server side not just in HTML via CSS. The repeater has an ID
, but I am having difficulty finding the table that it owns within the debugger. Considering how a repeater works I'm not sure its even possible. I gave the HTML table
an ID
and set it to runat="server"
, but it blew up with error
Unexpected end of file looking for tag.
How can I do it? Do I need to switch to a gridview? I could probably do this a lot easier with a gridview.
<asp:repeater id="repeaterId" runat="server">
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "col1")%></td>
<td nowrap="nowrap" align="left"><%# DataBinder.Eval(Container.DataItem, "col2")%></font></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate> <tr>
<td><%# DataBinder.Eval(Container.DataItem, "col1")%></td>
<td nowrap="nowrap" align="left"><%# DataBinder.Eval(Container.DataItem, "col2")%></td>
</AlternatingItemTemplate>
<HeaderTemplate>
<table id="rPhysicalTable" class="cssTable">
<tr class="aClass">
<th>col1</th>
<th>col2</th>
</tr>
</HeaderTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
We can hide the column of a html table using the repeaters ItemDataBound event
To do this, we specify the id for the table cells to be hidden and tag the cell as runat="server"
<asp:repeater id="repeaterId" runat="server">
<ItemTemplate>
<tr>
<td id="tdhideItem" runat="server"><%# DataBinder.Eval(Container.DataItem, "col1")%></td>
<td nowrap="nowrap" align="left"><%# DataBinder.Eval(Container.DataItem, "col2")%></font></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate> <tr>
<td id="tdhideAltItem" runat="server"><%# DataBinder.Eval(Container.DataItem, "col1")%></td>
<td nowrap="nowrap" align="left"><%# DataBinder.Eval(Container.DataItem, "col2")%></td>
</AlternatingItemTemplate>
<HeaderTemplate>
<table id="rPhysicalTable" class="cssTable">
<tr class="aClass">
<th id="thhideHeader" runat="server">col1</th>
<th>col2</th>
</tr>
</HeaderTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
The following vb.net code is specified as code behind
Protected Sub repeaterId_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repeaterId.ItemDataBound
If e.Item.ItemType = ListItemType.Item Then
DirectCast(e.Item.FindControl("tdhideItem"), HtmlTableCell).Visible = False
End If
If e.Item.ItemType = ListItemType.AlternatingItem
Then
DirectCast(e.Item.FindControl("tdhideAltItem"), HtmlTableCell).Visible = False
End If
If e.Item.ItemType = ListItemType.Header Then
DirectCast(e.Item.FindControl("thhideHeader"), HtmlTableCell).Visible = False
End If
End Sub
In the above code the first column of the table "col1" is set to be hidden
Following Tim Schmelter's advice I switched to a gridview. This way I can use
gridviewObj.Columns[index].Visible = false;
And thus avoid hiding multiple <td>
in a repeater.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With