Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to hide a column in an asp:repeater?

Tags:

asp.net

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>
like image 310
P.Brian.Mackey Avatar asked May 18 '11 21:05

P.Brian.Mackey


2 Answers

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

like image 77
Akhil Avatar answered Sep 20 '22 15:09

Akhil


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.

like image 25
P.Brian.Mackey Avatar answered Sep 21 '22 15:09

P.Brian.Mackey