Freigeben über


HtmlTable-Klasse

Ermöglicht den programmgesteuerten Zugriff auf das HTML-<table>-Element auf dem Server.

Namespace: System.Web.UI.HtmlControls
Assembly: System.Web (in system.web.dll)

Syntax

'Declaration
Public Class HtmlTable
    Inherits HtmlContainerControl
'Usage
Dim instance As HtmlTable
public class HtmlTable : HtmlContainerControl
public ref class HtmlTable : public HtmlContainerControl
public class HtmlTable extends HtmlContainerControl
public class HtmlTable extends HtmlContainerControl

Hinweise

Verwenden Sie das HtmlTable-Steuerelement zur programmgesteuerten Steuerung des HTML-<table>-Elements auf dem Server. Auf diese Weise können Sie eine Tabelle auf einer Webseite erstellen.

Sie können die Darstellung des <table>-Elements dynamisch ändern, indem Sie die BgColor-Eigenschaft, die Border-Eigenschaft, die BorderColor-Eigenschaft, die Height-Eigenschaft und die Width-Eigenschaft festlegen. Sie können auch steuern, wie der Inhalt einer Zelle angezeigt wird, indem Sie die Align-Eigenschaft, die CellPadding-Eigenschaft und die CellSpacing-Eigenschaft festlegen.

Die Zeilen des HtmlTable-Steuerelements sind in der Rows-Eigenschaft des Steuerelements gespeichert. Dies ermöglicht den programmgesteuerten Zugriff auf die einzelnen Zeilen der Tabelle.

Hinweis

Ein komplexes Tabellenmodell wird nicht unterstützt. Ein HtmlTable-Steuerelement, das <caption>-Elemente, <col>-Elemente, <colgroup>-Elemente, <tbody>-Elemente, <thead>-Elemente oder <tfoot>-Elemente schachtelt, ist unzulässig. Diese Elemente werden ohne Warnung entfernt und nicht in der HTML-Ausgabe angezeigt. Wenn Sie versuchen, diese Elemente des Tabellenmodells programmgesteuert zur Control.Controls-Auflistung des HtmlTable-Steuerelements hinzuzufügen, wird eine Ausnahme ausgelöst.

Eine Liste der anfänglichen Eigenschaftenwerte für eine Instanz von HtmlTable finden Sie unter HtmlTable-Konstruktor.

Thema Position
Gewusst wie: Erstellen und Bearbeiten von HTML-Tabellen in der Entwurfsansicht Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Auswählen von HTML-Tabellenelementen und -inhalten in Visual Web Developer Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Ändern der Größe von HTML-Tabellenelementen in Visual Web Developer Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Erstellen und Bearbeiten von HTML-Tabellen in der Entwurfsansicht Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Auswählen von HTML-Tabellenelementen und -inhalten in Visual Web Developer Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Hinzufügen von HTML-Serversteuerelementen zu einer Webseite mithilfe von ASP.NET-Syntax Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Ändern der Größe von HTML-Tabellenelementen in Visual Web Developer Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Dynamisches Hinzufügen von Zeilen und Zellen zu einem Table-Webserversteuerelement Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Programmgesteuertes Festlegen der Eigenschaften von HTML-Serversteuerelementen Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Erstellen und Bearbeiten von HTML-Tabellen in der Entwurfsansicht Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Auswählen von HTML-Tabellenelementen und -inhalten in Visual Web Developer Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Hinzufügen von HTML-Serversteuerelementen zu einer Webseite mithilfe von ASP.NET-Syntax Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Ändern der Größe von HTML-Tabellenelementen in Visual Web Developer Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Dynamisches Hinzufügen von Zeilen und Zellen zu einem Table-Webserversteuerelement Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Programmgesteuertes Festlegen der Eigenschaften von HTML-Serversteuerelementen Erstellen von ASP.NET-Webanwendungen in Visual Studio
Gewusst wie: Hinzufügen von HTML-Serversteuerelementen zu einer Webseite mithilfe von ASP.NET-Syntax Erstellen von ASP.NET-Webanwendungen
Gewusst wie: Dynamisches Hinzufügen von Zeilen und Zellen zu einem Table-Webserversteuerelement Erstellen von ASP.NET-Webanwendungen
Gewusst wie: Programmgesteuertes Festlegen der Eigenschaften von HTML-Serversteuerelementen Erstellen von ASP.NET-Webanwendungen

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie mit einem HtmlTable-Steuerelement Informationen in einer Tabelle angezeigt werden.

<%@ Page Language="VB" AutoEventWireup="True" %>

<script runat="server">

  Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)

    ' Set the properties of the HtmlTable with the
    ' user selections.
    Table1.BgColor = BgColorSelect.Value
    Table1.Border = CInt(BorderSelect.Value)
    Table1.BorderColor = BorderColorSelect.Value
    Table1.Height = HeightSelect.Value
    Table1.Width = WidthSelect.Value
    
  End Sub
  
</script>

<html>
<head>
   <title>HtmlTable Example</title>
</head>
<body>

   <form runat="server">

      <h3>HtmlTable Example</h3>

      <table id="Table1" 
             Border="1" 
             BorderColor="black" 
             runat="server">

         <tr>
            <th>
               Column 1
            </th>
            <th>
               Column 2
            </th>
            <th>
               Column 3
            </th>
         </tr>
         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
            <td>
               Cell 3
            </td>
         </tr>
         <tr>
            <td>
               Cell 4
            </td>
            <td>
               Cell 5
            </td>
            <td>
               Cell 6
            </td>
         </tr>

      </table>
      
      <hr>

      Select the display settings: <br><br>

      BgColor:
      <select id="BgColorSelect" 
              runat="server">

         <option Value="Red">Red</option>
         <option Value="Blue">Blue</option>
         <option Value="Green">Green</option>
         <option Value="Black">Black</option>
         <option Value="White" Selected>White</option>
        
      </select>

      &nbsp;&nbsp;

      Border:
      <select id="BorderSelect" 
              runat="server">

         <option Value="0">0</option>
         <option Value="1" Selected>1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>

      &nbsp;&nbsp;

      BorderColor:
      <select id="BorderColorSelect" 
              runat="server">

         <option Value="Red">Red</option>
         <option Value="Blue">Blue</option>
         <option Value="Green">Green</option>
         <option Value="Black" Selected>Black</option>
         <option Value="White">White</option>

      </select>

      <br><br>

      Height:
      <select id="HeightSelect" 
              runat="server">

         <option Value="0">0</option>
         <option Value="100">100</option>
         <option Value="150">150</option>
         <option Value="200">200</option>
         <option Value="250">250</option>

      </select>

      &nbsp;&nbsp;

      Width:
      <select id="WidthSelect" 
              runat="server">

         <option Value="0">0</option>
         <option Value="200">200</option>
         <option Value="250">250</option>
         <option Value="300">300</option>
         <option Value="350">350</option>

      </select>
       
      <br><br>
  
      <input type="button" 
             value="Generate Table"
             OnServerClick ="Button_Click" 
             runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>

<script runat="server">

  void Button_Click(Object sender, EventArgs e)
  {

    // Set the properties of the HtmlTable with the
    // user selections.
    Table1.BgColor = BgColorSelect.Value;
    Table1.Border = Convert.ToInt32(BorderSelect.Value);
    Table1.BorderColor = BorderColorSelect.Value;
    Table1.Height = HeightSelect.Value;
    Table1.Width = WidthSelect.Value;

  }

</script>

<html>
<head>
   <title>HtmlTable Example</title>
</head>
<body>

   <form runat="server">

      <h3>HtmlTable Example</h3>

      <table id="Table1" 
             Border="1" 
             BorderColor="black" 
             runat="server">

         <tr>
            <th>
               Column 1
            </th>
            <th>
               Column 2
            </th>
            <th>
               Column 3
            </th>
         </tr>
         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
            <td>
               Cell 3
            </td>
         </tr>
         <tr>
            <td>
               Cell 4
            </td>
            <td>
               Cell 5
            </td>
            <td>
               Cell 6
            </td>
         </tr>

      </table>
      
      <hr>

      Select the display settings: <br><br>

      BgColor:
      <select id="BgColorSelect" 
              runat="server">

         <option Value="Red">Red</option>
         <option Value="Blue">Blue</option>
         <option Value="Green">Green</option>
         <option Value="Black">Black</option>
         <option Value="White" Selected>White</option>
        
      </select>

      &nbsp;&nbsp;

      Border:
      <select id="BorderSelect" 
              runat="server">

         <option Value="0">0</option>
         <option Value="1" Selected>1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>

      &nbsp;&nbsp;

      BorderColor:
      <select id="BorderColorSelect" 
              runat="server">

         <option Value="Red">Red</option>
         <option Value="Blue">Blue</option>
         <option Value="Green">Green</option>
         <option Value="Black" Selected>Black</option>
         <option Value="White">White</option>

      </select>

      <br><br>

      Height:
      <select id="HeightSelect" 
              runat="server">

         <option Value="0">0</option>
         <option Value="100">100</option>
         <option Value="150">150</option>
         <option Value="200">200</option>
         <option Value="250">250</option>

      </select>

      &nbsp;&nbsp;

      Width:
      <select id="WidthSelect" 
              runat="server">

         <option Value="0">0</option>
         <option Value="200">200</option>
         <option Value="250">250</option>
         <option Value="300">300</option>
         <option Value="350">350</option>

      </select>
       
      <br><br>
  
      <input type="button" 
             value="Generate Table"
             OnServerClick ="Button_Click" 
             runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="JScript" AutoEventWireup="True" %>

<script runat="server">

  function Button_Click(sender : Object, e : EventArgs)
  { 

    // Set the properties of the HtmlTable with the
    // user selections.
    Table1.BgColor = BgColorSelect.Value;
    Table1.Border = Convert.ToInt32(BorderSelect.Value);
    Table1.BorderColor = BorderColorSelect.Value;
    Table1.Height = HeightSelect.Value;
    Table1.Width = WidthSelect.Value;

  }

</script>

<html>
<head>
   <title>HtmlTable Example</title>
</head>
<body>

   <form runat="server">

      <h3>HtmlTable Example</h3>

      <table id="Table1" 
             Border="1" 
             BorderColor="black" 
             runat="server">

         <tr>
            <th>
               Column 1
            </th>
            <th>
               Column 2
            </th>
            <th>
               Column 3
            </th>
         </tr>
         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
            <td>
               Cell 3
            </td>
         </tr>
         <tr>
            <td>
               Cell 4
            </td>
            <td>
               Cell 5
            </td>
            <td>
               Cell 6
            </td>
         </tr>

      </table>
      
      <hr>

      Select the display settings: <br><br>

      BgColor:
      <select id="BgColorSelect" 
              runat="server">

         <option Value="Red">Red</option>
         <option Value="Blue">Blue</option>
         <option Value="Green">Green</option>
         <option Value="Black">Black</option>
         <option Value="White" Selected>White</option>
        
      </select>

      &nbsp;&nbsp;

      Border:
      <select id="BorderSelect" 
              runat="server">

         <option Value="0">0</option>
         <option Value="1" Selected>1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>

      &nbsp;&nbsp;

      BorderColor:
      <select id="BorderColorSelect" 
              runat="server">

         <option Value="Red">Red</option>
         <option Value="Blue">Blue</option>
         <option Value="Green">Green</option>
         <option Value="Black" Selected>Black</option>
         <option Value="White">White</option>

      </select>

      <br><br>

      Height:
      <select id="HeightSelect" 
              runat="server">

         <option Value="0">0</option>
         <option Value="100">100</option>
         <option Value="150">150</option>
         <option Value="200">200</option>
         <option Value="250">250</option>

      </select>

      &nbsp;&nbsp;

      Width:
      <select id="WidthSelect" 
              runat="server">

         <option Value="0">0</option>
         <option Value="200">200</option>
         <option Value="250">250</option>
         <option Value="300">300</option>
         <option Value="350">350</option>

      </select>
       
      <br><br>
  
      <input type="button" 
             value="Generate Table"
             OnServerClick ="Button_Click" 
             runat="server"/>

   </form>

</body>
</html>

Im folgenden Codebeispiel wird das dynamische Erstellen eines HtmlTable-Steuerelements veranschaulicht.

<%@ Page Language="VB" %>

<script runat="server">

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    Dim i As Integer
    Dim j As Integer
    Dim row As HtmlTableRow
    Dim cell As HtmlTableCell

    ' Get the number of rows and columns selected by the user.
    Dim numrows As Integer = CInt(Select1.Value)
    Dim numcells As Integer = CInt(Select2.Value)

    ' Iterate through the rows.
    For j = 0 To numrows - 1

      ' Create a new row.
      row = New HtmlTableRow()

      ' Provide a different background color for alternating rows.
      If (j Mod 2) = 1 Then
        row.BgColor = "Gray"
      End If

      ' Iterate through the cells of a row.
      For i = 0 To numcells - 1
        
        ' Create a new cell and add it to the HtmlTableRow
        ' Cells collection.
        cell = New HtmlTableCell()
        cell.Controls.Add(New LiteralControl("row " & _
                                          j.ToString() & _
                                          ", cell " & _
                                          i.ToString()))
        row.Cells.Add(cell)
            
      Next i

      ' Add the row to the HtmlTable Rows collection.
      Table1.Rows.Add(row)
         
    Next j
      
  End Sub

</script>

<html>
<head>
   <title>HtmlTable Example</title>
</head>
<body>

   <form runat="server">

      <h3>HtmlTable Example</h3>

      <table id="Table1" 
             CellPadding="5" 
             CellSpacing="0" 
             Border="1" 
             BorderColor="black" 
             runat="server"/>
        
      <hr>

      Select the number of rows and columns to create: <br><br>

      Table rows:
      <select id="Select1" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>

      &nbsp;&nbsp;

      Table cells:
      <select id="Select2" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>
       
      <br><br>
  
      <input type="submit" 
             value="Generate Table" 
             runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="C#" %>

<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    // Get the number of rows and columns selected by the user.
    int numrows = Convert.ToInt32(Select1.Value);
    int numcells = Convert.ToInt32(Select2.Value);

    // Iterate through the rows.
    for (int j = 0; j < numrows; j++)
    {

      // Create a new row and add it to the Rows collection.
      HtmlTableRow row = new HtmlTableRow();

      // Provide a different background color for alternating rows.
      if (j % 2 == 1)
        row.BgColor = "Gray";

      // Iterate through the cells of a row.
      for (int i = 0; i < numcells; i++)
      {
        // Create a new cell and add it to the HtmlTableRow 
        // Cells collection.
        HtmlTableCell cell = new HtmlTableCell();
        cell.Controls.Add(new LiteralControl("row " +
                          j.ToString() +
                          ", cell " +
                          i.ToString()));
        row.Cells.Add(cell);
      }

      // Add the row to the HtmlTable Rows collection.
      Table1.Rows.Add(row);
    }
  }

</script>

<html>
<head>
   <title>HtmlTable Example</title>
</head>
<body>

   <form runat="server">

      <h3>HtmlTable Example</h3>

      <table id="Table1" 
             CellPadding="5" 
             CellSpacing="0" 
             Border="1" 
             BorderColor="black" 
             runat="server"/>
        
      <hr>

      Select the number of rows and columns to create: <br><br>

      Table rows:
      <select id="Select1" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>

      &nbsp;&nbsp;

      Table cells:
      <select id="Select2" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>
       
      <br><br>
  
      <input type="submit" 
             value="Generate Table" 
             runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="JScript" %>

<script runat="server">

  function Page_Load(sender : Object, e : EventArgs) 
  {

    // Get the number of rows and columns selected by the user.
    var numrows : int = Convert.ToInt32(Select1.Value);
    var numcells : int = Convert.ToInt32(Select2.Value);

    // Iterate through the rows.
    for (var j : int = 0; j < numrows; j++) 
    {

      // Create a new row and add it to the Rows collection.
      var row : HtmlTableRow = new HtmlTableRow();

      // Provide a different background color for alternating rows.
      if (j%2 == 1)
        row.BgColor="Gray";

      // Iterate through the cells of a row.
      for (var i : int = 0; i < numcells; i++) 
      {
        // Create a new cell and add it to the HtmlTableRow 
        // Cells collection.
        var cell : HtmlTableCell = new HtmlTableCell();
        cell.Controls.Add(new LiteralControl("row " + 
                      j.ToString() + 
                      ", cell " +
                     i.ToString()));
        row.Cells.Add(cell);
      }
      
      // Add the row to the HtmlTable Rows collection.
      Table1.Rows.Add(row);
    }
  }

</script>

<html>
<head>
   <title>HtmlTable Example</title>
</head>
<body>

   <form runat="server">

      <h3>HtmlTable Example</h3>

      <table id="Table1" 
             CellPadding="5" 
             CellSpacing="0" 
             Border="1" 
             BorderColor="black" 
             runat="server"/>
        
      <hr>

      Select the number of rows and columns to create: <br><br>

      Table rows:
      <select id="Select1" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>

      &nbsp;&nbsp;

      Table cells:
      <select id="Select2" 
              runat="server">

         <option Value="1">1</option>
         <option Value="2">2</option>
         <option Value="3">3</option>
         <option Value="4">4</option>
         <option Value="5">5</option>

      </select>
       
      <br><br>
  
      <input type="submit" 
             value="Generate Table" 
             runat="server"/>

   </form>

</body>
</html>

.NET Framework-Sicherheit

Vererbungshierarchie

System.Object
   System.Web.UI.Control
     System.Web.UI.HtmlControls.HtmlControl
       System.Web.UI.HtmlControls.HtmlContainerControl
        System.Web.UI.HtmlControls.HtmlTable

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

HtmlTable-Member
System.Web.UI.HtmlControls-Namespace
HtmlContainerControl-Klasse
HtmlTableCell
HtmlTableCellCollection
HtmlTableRow
HtmlTableRowCollection

Weitere Ressourcen

HTML-Serversteuerelemente