ASP.NET Tutorial/ASP.net Controls/Table

Материал из .Net Framework эксперт
Версия от 12:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

asp:Table

<%@ Page Language="vb" %>
<html>
<head>
   <title>Control Properties Example</title>
   <script runat="server">
      Sub Page_Load()
         Label2.BackColor = System.Drawing.Color.LightBlue
      End Sub
   </script>
</head>
<body>
   <h1>Control Properties Example</h1>
   <form runat="server">
      <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               Default Label:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:label id="Label1" runat="server">
                  Hello, World!
               </asp:label>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               Label with Properties:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:label id="Label2" font-name="arial" runat="server">
                  Hello, World!
               </asp:label>
            </asp:tablecell>
         </asp:tablerow>
      </asp:table>
   </form>
</body>
</html>


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]));
      }
  }
}


Dynamically adding rows to the table (C#)

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
       TableRow tr = new TableRow();
       TableCell fname = new TableCell();
       fname.Text = "A";
       tr.Cells.Add(fname);
            
       TableCell lname = new TableCell();
       lname.Text = "B";
       tr.Cells.Add(lname);
            
       Table1.Rows.Add(tr);
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Table</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="Table1" Runat="server">
           <asp:TableRow ID="TableRow1" Runat="server" Font-Bold="True"
            ForeColor="Black" BackColor="Silver">
              <asp:TableHeaderCell>First Name</asp:TableHeaderCell>
              <asp:TableHeaderCell>Last Name</asp:TableHeaderCell>
           </asp:TableRow>
           <asp:TableRow>
              <asp:TableCell>Bill</asp:TableCell>
              <asp:TableCell>Evjen</asp:TableCell>
           </asp:TableRow>
          <asp:TableRow>
              <asp:TableCell>Devin</asp:TableCell>
              <asp:TableCell>Rader</asp:TableCell>
           </asp:TableRow>
        </asp:Table>
    </div>
    </form>
</body>
</html>


Dynamically adding rows to the table (VB)

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim tr As New TableRow()
       
        Dim fname As New TableCell()
        fname.Text = "Scott"
        tr.Cells.Add(fname)
        
        Dim lname As New TableCell()
        lname.Text = "Hanselman"
        tr.Cells.Add(lname)
        
        Table1.Rows.Add(tr)
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Table</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="Table1" Runat="server">
           <asp:TableRow ID="TableRow1" Runat="server" Font-Bold="True"
            ForeColor="Black" BackColor="Silver">
              <asp:TableHeaderCell>First Name</asp:TableHeaderCell>
              <asp:TableHeaderCell>Last Name</asp:TableHeaderCell>
           </asp:TableRow>
           <asp:TableRow>
              <asp:TableCell>A</asp:TableCell>
              <asp:TableCell>B</asp:TableCell>
           </asp:TableRow>
          <asp:TableRow>
              <asp:TableCell>C</asp:TableCell>
              <asp:TableCell>D</asp:TableCell>
           </asp:TableRow>
        </asp:Table>
    </div>
    </form>
</body>
</html>


Repeater control in asp:Table

<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<html>
<head>
   <title>List Control Example</title>
   <script runat="server">
      Sub Page_Load()
         MyRepeater.DataSource = CreateData()
         MyRepeater.DataBind()
      End Sub
      Function CreateData() As DataTable
         Dim DT As New DataTable()
         Dim Row1, Row2, Row3, Row4 As DataRow
         DT.Columns.Add(New DataColumn("name", System.Type.GetType("System.String")))
         DT.Columns.Add(New DataColumn("city", System.Type.GetType("System.String")))
         Row1 = DT.NewRow()
         Row1("name") = "A"
         Row1("city") = "B"
         DT.Rows.Add(Row1)
         Row2 = DT.NewRow()
         Row2("name") = "C"
         Row2("city") = "D"
         DT.Rows.Add(Row2)
         Row3 = DT.NewRow()
         Row3("name") = "E"
         Row3("city") = "F"
         DT.Rows.Add(Row3)
         Row4 = DT.NewRow()
         Row4("name") = "H"
         Row4("city") = "I"
         DT.Rows.Add(Row4)
         Return DT
      End Function
   </script>
</head>
<body>
   <h1>Control Templates Example</h1>
   <form runat="server">
      <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               Repeater Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:repeater id="MyRepeater" runat="server">
                  <headertemplate>
                     <h3>Famous Composers" Birthplaces</h3>
                     <table cellpadding="5" cellspacing="0">
                        <tr>
                           <td>Name<hr/></td>
                           <td>City<hr/></td>
                        </tr>
                  </headertemplate>
                  <itemtemplate>
                        <tr>
                           <td><strong><%# DataBinder.Eval(Container.DataItem, "name") %><strong></td>
                           <td><%# DataBinder.Eval(Container.DataItem, "city") %></td>
                        </tr>
                  </itemtemplate>
                  <footertemplate>
                     </table>
                  </footertemplate>
               </asp:repeater>
            </asp:tablecell>
         </asp:tablerow>
      </asp:table>
   </form>
</body>
</html>


TableRow and TableCell

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TableTest" %>
<!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 Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="container">
    <h1>Using Table Control</h1>
      <h2>Table via Markup</h2>
      
      <asp:Table id="tabMarkup" runat="server" >
        <asp:TableRow HorizontalAlign="Center"  
                      TableSection="TableHeader"
                  BackColor="#FFFF80" 
                  Font-Bold="True">
          <asp:TableCell Width="100px" Text="First"> 
          </asp:TableCell>
          <asp:TableCell Width="100px" Text="Second">
          </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow HorizontalAlign="Center" 
                  BackColor="#FFFFC0">
          <asp:TableCell Text="10.5"></asp:TableCell>
          <asp:TableCell Text="36.5"></asp:TableCell>
        </asp:TableRow>
        <asp:TableRow HorizontalAlign="Center"
                  BackColor="#FFFFC0">
          <asp:TableCell Text="45.3"></asp:TableCell>
          <asp:TableCell Text="16.5"></asp:TableCell>
        </asp:TableRow>
      </asp:Table>
      
      <h2>Table via Programming</h2>
      <asp:Table id="tabProgramming" runat="server"></asp:Table>
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class TableTest : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
     TableRow tr1 = new TableRow();
       tr1.BackColor = System.Drawing.Color.Goldenrod;
     tr1.Font.Bold = true;
     TableCell tc1a = new TableCell();
     tc1a.Text = "Author";
     tc1a.Width = 100;
     TableCell tc1b = new TableCell();
     tc1b.Text = "Nationality";
     tc1b.Width = 100;
     tr1.Cells.Add(tc1a);
     tr1.Cells.Add(tc1b);
     TableRow tr2 = new TableRow();
       tr2.BackColor = System.Drawing.Color.LightGoldenrodYellow;
     TableCell tc2a = new TableCell();
     tc2a.Text = "A";
     TableCell tc2b = new TableCell();
     tc2b.Text = "B";
     tr2.Cells.Add(tc2a);
     tr2.Cells.Add(tc2b);
     tabProgramming.Rows.Add(tr1);
     tabProgramming.Rows.Add(tr2);
   }
}


Using the new Caption attribute (C#)

<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server">
    <title>Table Server Control</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Table ID="Table1" Runat="server"
         Caption="<b>Table 1:</b> This is an example of a caption above a table."
         BackColor="Gainsboro">
            <asp:TableRow ID="Tablerow1" Runat=server>
                <asp:TableCell ID="Tablecell1" Runat="server">Lorem ipsum dolor sit 
                 amet, consectetuer adipiscing elit. Duis vel justo. Aliquam 
                 adipiscing. In mattis volutpat urna. Donec adipiscing, nisl eget 
                 dictum egestas, felis nulla ornare ligula, ut bibendum pede augue 
                 eu augue. Sed vel risus nec urna pharetra imperdiet. Aenean 
                 semper. Sed ullamcorper auctor sapien. Suspendisse luctus. Ut ac 
                 nibh. Nam lorem. Aliquam dictum aliquam purus.</asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </form>
</body>
</html>