0

Solution to add 2 cells to a asp:Table TableRow. I'm pulling the data for the table from a database and removing the comma separated items with the string function.

protected void listView_Bids(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        TextBox TextBoxHighlights = (TextBox)e.Item.FindControl("TextBox2");
        Table FindHighlightTable = (Table)e.Item.FindControl("bidHighlightTable");

        if (TextBoxHighlights != null)
        {

            string benefits = (TextBoxHighlights.Text.ToString());

            TableRow row = new TableRow();
            int i = 0;

            string[] words = benefits.Split(',');
            foreach (string word in words)
            {

                if (i == 0 || i % 2 == 0)
                {
                    row = new TableRow();
                }
                TableCell cell1 = new TableCell();
                cell1.Text = word.ToString();
                row.Cells.Add(cell1);

                i++;

                if (i % 2 == 0)
                {
                    FindHighlightTable.Rows.Add(row);
                }

            }
        }
    }
}

Here is the that I have in my ListView:

<asp:Table ID="bidHighlightTable" CssClass="table table-striped table-bordered bid-highlight-table" runat="server">
   <asp:TableRow runat="server">
      <asp:TableCell runat="server"></asp:TableCell>
      <asp:TableCell runat="server"></asp:TableCell>
   </asp:TableRow>
</asp:Table>
brandozz
  • 1,059
  • 6
  • 20
  • 38

1 Answers1

1

I would recommend looking at an alternative control which can also provide a standard html table as the output.

I would suggest in your case looking at a DataList (another asp.net control).

With this control you can set the RepeatColumns property which in your case you would set to 2. This would mean your content will fill both columns when populating.

A DataList control renders as a standard HTML table the same in which you are trying to achieve with your sample code.

Edmund G
  • 545
  • 1
  • 5
  • 9
  • Thanks I will look into it. I friend of mine actually helped me out with this one and I've edited my post to show the solution. – brandozz Oct 04 '13 at 19:32