A Generic List Box.
A Generic List Box.
An Item
Another Item
Testing a Table Cell
Sea Monkeys are pretty strange animals.
I'm a table cell, you are not!

Name:
Value:
(Add to ListBox and Table)

Notes:
  • I've never liked ASP.NET tables. They've always seemed clunky and not as efficient as regular tables.
  • Check out my Efficiency section, I have an example there that deals with HTML Tables vs. ASP.NET tables.
  • Have you noticed when a new Item is added, that the ASP.NET table doesn't retain the previous item while the ListBox does?



Front end code

    8             <table border="0" style="height: 200px;">
    9                 <tr>
   10                     <td style="width: 400px;">
   11                         A Generic List Box.
   12                         <br />
   13                         <asp:ListBox ID="lstDemo" runat="server" Style="width: 100%; height: 200px;">
   14                             <asp:ListItem Value="1">An Item</asp:ListItem>
   15                             <asp:ListItem Value="35">Another Item</asp:ListItem>
   16                             <asp:ListItem Value="34">Testing an Item</asp:ListItem>
   17                             <asp:ListItem Value="1">Monkeys make bad programmers</asp:ListItem>
   18                             <asp:ListItem Value="342">Unless they're ninja monkeys</asp:ListItem>
   19                             <asp:ListItem Selected="true">All</asp:ListItem>
   20                         </asp:ListBox>
   21                     </td>
   22                     <td style="width: 400px;">
   23                         A Generic List Box.
   24                         <br />
   25                         <asp:Table ID="tblDemo" runat="server" Style="width: 100%;">
   26                             <asp:TableRow runat="server">
   27                                 <asp:TableCell>An Item</asp:TableCell>
   28                             </asp:TableRow>
   29                             <asp:TableRow runat="server">
   30                                 <asp:TableCell>Another Item</asp:TableCell>
   31                             </asp:TableRow>
   32                             <asp:TableRow runat="server">
   33                                 <asp:TableCell>Testing a Table Cell</asp:TableCell>
   34                             </asp:TableRow>
   35                             <asp:TableRow runat="server">
   36                                 <asp:TableCell>Sea Monkeys are pretty strange animals.</asp:TableCell>
   37                             </asp:TableRow>
   38                             <asp:TableRow runat="server">
   39                                 <asp:TableCell>I'm a table cell, you are not!</asp:TableCell>
   40                             </asp:TableRow>
   41                         </asp:Table>
   42                     </td>
   43                 </tr>
   44             </table>
   45             <br />
   46             Name:
   47             <asp:TextBox ID="txtName" runat="server" />
   48             <br />
   49             Value:
   50             <asp:TextBox ID="txtValue" runat="server" />
   51             <br />
   52             <asp:Button ID="btnAddListItem" runat="server" Text="Add List Item" OnClick="btnAddListItem_Click" />
   53             (Add to ListBox and Table)
   54             <br />


Source Code.
    1 using System;
    2 using System.Data;
    3 using System.Configuration;
    4 using System.Collections;
    5 using System.Web;
    6 using System.Web.Security;
    7 using System.Web.UI;
    8 using System.Web.UI.WebControls;
    9 using System.Web.UI.WebControls.WebParts;
   10 using System.Web.UI.HtmlControls;
   11 namespace BicNet.Projects.mcp.chapter4
   12 {
   13     public partial class TablesAndLists : System.Web.UI.Page
   14     {
   15         protected void Page_Load(object sender, EventArgs e)
   16         {
   17 
   18         }
   19 
   20         protected void btnAddListItem_Click(object sender, EventArgs e)
   21         {
   22             lstDemo.Items.Add(new ListItem(txtName.Text,txtValue.Text));
   23             TableRow tr = new TableRow();
   24             TableCell tc = new TableCell();
   25             tc.Text =  "Name : " + txtName.Text + "   |   Value : " + txtValue.Text;
   26             tr.Cells.Add(tc);            
   27             tblDemo.Rows.Add(tr);
   28             //Clear the text boxes
   29             txtName.Text = string.Empty;
   30             txtValue.Text = string.Empty;
   31 
   32         }
   33 }
   34 }