Mega Code Archive

 
Categories / ASP.Net Tutorial / ASP Net Controls
 

Create table programatically

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"  Inherits="Default_aspx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Table Control</title> </head> <body>     <form id="form1" runat="server">     <div>     <h1>Table Control</h1>        <tr>          <td>           <strong>Select a Font Style:</strong>          </td>          <td>            <asp:CheckBoxList ID="cblFontStyle" runat="server" AutoPostBack="True" CellPadding="5" CellSpacing="10" RepeatColumns="3" OnInit="cblFontStyle_Init">            </asp:CheckBoxList>          </td>        </tr>        <tr>          <td>           <strong>Select a Font Size:</strong>          </td>          <td>            <asp:RadioButtonList ID="rblSize" runat="server" AutoPostBack="True" CellSpacing="20" RepeatColumns="3" RepeatDirection="Horizontal">                           <asp:ListItem text="10pt" value="10"/>                             <asp:ListItem text="12pt" value="12" selected = "true"/>                             <asp:ListItem text="14pt" value="14"/>                             <asp:ListItem text="16pt" value="16"/>                             <asp:ListItem text="18pt" value="18"/>                             <asp:ListItem text="24pt" value="24"/>              </asp:RadioButtonList>          </td>        </tr>      <asp:Table ID="tbl" runat="server"  BackImageUrl="Sunflower.jpg" Font-Names="Times New Roman" Font-Size="12" GridLines="Both" CellPadding="10" CellSpacing="5" HorizontalAlign="Left" Width="100%">       <asp:TableRow HorizontalAlign="Left">         <asp:TableHeaderCell>Font Family</asp:TableHeaderCell>         <asp:TableHeaderCell Width="80%">Sample Text</asp:TableHeaderCell>       </asp:TableRow>      </asp:Table>          </div>     </form> </body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing;        //  necessary for FontFamily using System.Drawing.Text;      //  necessary for Fonts public partial class Default_aspx : System.Web.UI.Page  {     protected void Page_Load(object sender, EventArgs e)     {       string str = "The quick brown fox jumped over the lazy dogs.";       int i = 0;       bool boolUnder = false;       bool boolOver = false;       bool boolStrike = false;       foreach(ListItem li in cblFontStyle.Items)       {          if (li.Selected == true)          {             switch (li.Value)             {                case "u":                   boolUnder = true;                   break;                case "o":                   boolOver = true;                   break;                case "s":                   boolStrike = true;                   break;             }          }       }       int size = Convert.ToInt32(rblSize.SelectedItem.Value);       InstalledFontCollection ifc = new InstalledFontCollection(  );       foreach( FontFamily ff in ifc.Families )       {          TableRow r = new TableRow(  );          TableCell cFont = new TableCell(  );             cFont.Controls.Add(new LiteralControl(ff.Name));          r.Cells.Add(cFont);          TableCell cText = new TableCell(  );          Label lbl = new Label(  );          lbl.Text = str;             i++;          lbl.ID = "lbl" + i.ToString(  );          lbl.Font.Name = ff.Name;          if (boolUnder)             lbl.Font.Underline = true;          if (boolOver)             lbl.Font.Overline = true;          if (boolStrike)             lbl.Font.Strikeout = true;          lbl.Font.Size = size;          cText.Controls.Add(lbl);          r.Cells.Add(cText);          tbl.Rows.Add(r);       }    }     protected void  cblFontStyle_Init(object sender, EventArgs e)   {       string[] FontStyle = {"Underline","OverLine", "Strikeout"};       string[] Code = {"u","o","s"};       for (int i = 0; i < FontStyle.GetLength(0); i++)       {          this.cblFontStyle.Items.Add(new ListItem(FontStyle[i],Code[i]));       }   } }