views:

718

answers:

3

I have a Repeater that contains a Table. I want to hide the some tablecells of the table in repeater item template Here is the ASPX source code:

  <ItemTemplate>
  <table style="width: 100%" align="center">
    <tr>
      <td style="width: 60px;" align="center">
        <img src="upload/companylogo/<%# Eval("companylogo") %>" />
      </td>
      <td align="left">
        <asp:Label runat="server" CssClass="lblcname" ID="Label1" Text='<%# Eval("companyname") %>' /></td>
      <td align="right">
        <asp:Label runat="server" ID="Label2" Text='<%# Eval("city") %>' /></td>
    </tr>
    <tr>
      <td runat="server" id="address" colspan="3">
        <asp:Label runat="server" ID="Label3" Text='<%# Eval("address") %>' />
      </td>
    </tr>
    <tr>
      <td colspan="3" align="right" id="vp" runat="server">
        <a href='nfonews.aspx?id=<%# Eval("mpid") %>'>view Profile</a>
        &raquo; Send Inquiry </td>
    </tr>
    <tr>
      <td colspan="3" style="height: 20px; background-image: url(image/stripe_head_bg.png)"></td>
    </tr>
  </table>
</ItemTemplate>

And my code-behind:

SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
  dr.Read();
  if (dr["payment"].ToString()  == "Yes")
  {
    Repeater1.DataBind();
    if (Repeater1.Items.Count == 0)
    {
      Repeater1.Visible = false;
    }
    else
    {
      Repeater1.Visible = true;
    }
  }
}
+2  A: 

In the ItemDataBound event of the grid, use FindControl to find the cell.

Add the attribute; onitemdatabound="myRepeater_ItemDataBound"

then in code behind

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   ListItemType rowType = (ListItemType)e.Item.ItemType;
   if (rowType == ListItemType.Pager || rowType == ListItemType.Header || rowType == ListItemType.Footer)
      return;

   TableCell cell = (TableCell)e.Item.FindControl("address");
}
Dead account
This will require the OP to substantially change the Table... into a Table webserver control.
Cerebrus
Sorry, I meant to say "HtmlTableCell" to refer to the non ASP html control
Dead account
A: 

The simplest way could be to use the visible attribute of the td and assign it a value based on a server side expression. Since you have not mentioned the conditions in which you wish to show/hide particular columns, the below code is an example of the possible way:

<table style="width: 100%" align="center">
  <tr>
    <td style="width: 60px;" align="center" runat="server" visible="<%#showCompanyLogo %>">
      <img src="upload/companylogo/<%# Eval("companylogo") %>" />
    </td>
    <td align="left" runat="server" visible="<%#showCompanyName %>">
      <asp:Label runat="server" CssClass="lblcname" ID="Label1" Text='<%# Eval("companyname") %>' /></td>
    <td align="right" runat="server" visible="<%#showCity %>">
      <asp:Label runat="server" ID="Label2" Text='<%# Eval("city") %>' /></td>
  </tr>
...
</table>

showCompanyLogo, showCompanyName and showCity are boolean variables (with Protected access level) declared in the code-behind and set according to whatever conditions you want to evaluate.

Note that your table cells will need to be runat="server" for the server expression to be evaluated.

Alternatively, you can use the solution presented by Ian, but you will need to convert your entire table into a Table Webserver control with every single element marked runat="server".

Cerebrus
A: 

I am just wondering how we can change the value of cell in a repeate, I tried to use the above expamle to do that but it didn't work for me, and should I add a raunt server property to the repeater's rows.

thanks, hanan