Controllo HtmlTableCell

Crea un controllo lato server di cui viene eseguito il mapping sugli elementi HTML <td> e <th> e consente di modificare una cella in una tabella.

<td or th id="programmaticID"          align="alignmentofcontentincell"          bgcolor="bgcolor"          bordercolor="bordercolor"          colspan="#ofcolscellspans"          height="cellheight"          nowrap="True | False"          rowspan="#ofrowscellspans"          valign="vertalignmentofcellcontent"          width="cellwidth">CellContent</td or /th>

Osservazioni

Utilizzare la classe HtmlTableCell per eseguire la programmazione sugli elementi HTML <td> e <th>. Un elemento <td> rappresenta una cella di dati, mentre l'elemento <th> rappresenta una cella di intestazione. Si noti che i contenuti di una cella <th> sono sempre in grassetto e centrati.

La classe HtmlTableCell consente di controllare l'aspetto di ciascuna cella. È possibile controllare il colore di sfondo, il colore e l'altezza del bordo e la larghezza della cella impostando rispettivamente le proprietà BgColor, BorderColor, Height e Width.

Nota   Tutte le celle nella stessa riga condividono la stessa altezza. La cella più alta di una riga determina l'altezza di tutte le celle nella riga.

L'allineamento orizzontale e verticale del contenuto delle celle viene controllato impostando rispettivamente le proprietà Align e VAlign. È inoltre possibile specificare se il testo deve continuare automaticamente nella riga successiva della cella impostando la proprietà NoWrap.

La classe HtmlTableCell consente di espandere le celle impostando le proprietà ColSpan e RowSpan. La proprietà ColSpan consente di controllare quante colonne sono occupate da una cella, mentre la proprietà rowspan specifica il numero di righe occupate da una cella.

Nota   Quando si espandono le celle, assicurarsi che ogni riga nella tabella abbia la stessa lunghezza. Assicurarsi inoltre che ogni colonna abbia la stessa altezza. In caso contrario, l'aspetto della tabella potrebbe risultare diverso dal previsto.

Esempio

Nell'esempio seguente viene descritto come utilizzare l'oggetto HtmlTableCell per modificare il contenuto di una cella nel controllo HtmlTable.

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

<html>
<head>

   <script runat="server">

      Sub Button_Click(sender As Object, e As EventArgs) 
      
         Dim i As Integer
         Dim j As Integer

         ' Iterate through the rows of the table.
         For i=0 To Table1.Rows.Count - 1

            ' Iterate through the cells of a row.       
            For j=0 To Table1.Rows(i).Cells.Count - 1
            
               ' Change the inner HTML of the cell.
               Table1.Rows(i).Cells(j).InnerHtml = "Row " & i.ToString() _
                                                   & ", Column " & _
                                                   j.ToString() 
            Next j

         Next i

      End Sub

   </script>

</head>
<body>

   <form runat="server">

      <h3>HtmlTableCell Example</h3>

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

         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
         </tr>
         <tr>
            <td>
               Cell 3
            </td>
            <td>
               Cell 4
            </td>
         </tr>

      </table>


      <br><br>
  
      <input type="button" 
             value="Change Table Contents"
             OnServerClick = "Button_Click" 
             runat="server"/>

   </form>

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

<html>
<head>

   <script runat="server">

      void Button_Click(Object sender, EventArgs e) 
      {

         // Iterate through the rows of the table.
         for (int i=0; i<=Table1.Rows.Count - 1; i++)
         {

            // Iterate through the cells of a row.
            for (int j=0; j<=Table1.Rows[i].Cells.Count - 1; j++)
            {
               // Change the inner HTML of the cell.
               Table1.Rows[i].Cells[j].InnerHtml = "Row " + i.ToString() + 
                                                   ", Column " + 
                                                   j.ToString(); 
            }

         }

      }

   </script>

</head>
<body>

   <form runat="server">

      <h3>HtmlTableCell Example</h3>

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

         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
         </tr>
         <tr>
            <td>
               Cell 3
            </td>
            <td>
               Cell 4
            </td>
         </tr>

      </table>


      <br><br>
  
      <input type="button" 
             value="Change Table Contents"
             OnServerClick = "Button_Click" 
             runat="server"/>

   </form>

</body>
</html>

Vedere anche

Sintassi ASP.NET per controlli HTML | Classe HtmlTableCell | Classe HtmlTable | Spazio dei nomi System.Web.UI.HtmlControls