DataList.ItemStyle Propiedad

Definición

Obtiene las propiedades de estilo para los elementos del control DataList.

public:
 virtual property System::Web::UI::WebControls::TableItemStyle ^ ItemStyle { System::Web::UI::WebControls::TableItemStyle ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public virtual System.Web.UI.WebControls.TableItemStyle ItemStyle { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.ItemStyle : System.Web.UI.WebControls.TableItemStyle
Public Overridable ReadOnly Property ItemStyle As TableItemStyle

Valor de propiedad

TableItemStyle

Objeto TableItemStyle que contiene las propiedades de estilo de los elementos del control DataList. El valor predeterminado es un objeto TableItemStyle vacío.

Atributos

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar la ItemStyle propiedad para especificar un color de fondo diferente para los elementos del DataList control .

Nota

En el ejemplo de código siguiente se usa el modelo de código de un solo archivo y es posible que no funcione correctamente si se copia directamente en un archivo de código subyacente. Este ejemplo de código debe copiarse en un archivo de texto vacío que tenga una extensión .aspx. Para obtener más información sobre el modelo de código de Web Forms, vea modelo de código de página ASP.NET Web Forms.


<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <script runat="server">
 
      ICollection CreateDataSource() 
      {
      
         // Create sample data for the DataList control.
         DataTable dt = new DataTable();
         DataRow dr;
 
         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(String)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
         dt.Columns.Add(new DataColumn("ImageValue", typeof(String)));
 
         // Populate the table with sample values.
         for (int i = 0; i < 9; i++) 
         {
            dr = dt.NewRow();
 
            dr[0] = i;
            dr[1] = "Description for item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
            dr[3] = "Image" + i.ToString() + ".jpg";
 
            dt.Rows.Add(dr);
         }
 
         DataView dv = new DataView(dt);
         return dv;
      }
  
      void Page_Load(Object sender, EventArgs e) 
      {

         // Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 
         {
            ItemsList.DataSource = CreateDataSource();
            ItemsList.DataBind();
         }

      }

      void Selection_Change(Object sender, EventArgs e)
      {

         // Set the background color for the heading and footer sections
         // of the DataList control.
         ItemsList.ItemStyle.BackColor = 
             System.Drawing.Color.FromName(ItemList.SelectedItem.Value);
         ItemsList.AlternatingItemStyle.BackColor = 
             System.Drawing.Color.FromName(AltItemList.SelectedItem.Value);

      }
 
   </script>
 
<head runat="server">
    <title>DataList ItemStyle and AlternatingItemStyle Example</title>
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>DataList ItemStyle and AlternatingItemStyle Example</h3>

      Select background colors for the items and alternating items.

      <br /><br />
 
      <asp:DataList id="ItemsList"
           BorderColor="black"
           CellPadding="5"
           CellSpacing="5"
           RepeatDirection="Vertical"
           RepeatLayout="Table"
           RepeatColumns="3"
           ShowFooter="True"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <ItemStyle BackColor="White">
         </ItemStyle>

         <AlternatingItemStyle BackColor="White">
         </AlternatingItemStyle>

         <HeaderTemplate>

            List of items

         </HeaderTemplate>
               
         <ItemTemplate>

            Description: <br />
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>

            <br />

            Price: <%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>

            <br />

            <asp:Image id="ProductImage"
                 AlternatingText='<%# DataBinder.Eval(Container.DataItem, "StringValue") %>'
                 ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
                 runat="server"/>

         </ItemTemplate>

         <AlternatingItemTemplate>

            <asp:Image id="ProductImage"
                 AlternatingText='<%# DataBinder.Eval(Container.DataItem, "StringValue") %>'
                 ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
                 runat="server"/>

            <br />

            Description: <br />
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>

            <br />

            Price: <%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>       

         </AlternatingItemTemplate>
 
      </asp:DataList>

      <hr />

      <table cellpadding="5">

         <tr>

            <td>

               Item BackColor:
 
            </td>

            <td>

               Alternating item BackColor:

            </td>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="ItemList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>
 
            </td>

            <td>

               <asp:DropDownList id="AltItemList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>

            </td>

         </tr>

      </table>
 
   </form>
 
</body>
</html>

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <script runat="server">
 
      Function CreateDataSource() As ICollection 
      
         ' Create sample data for the DataList control.
         Dim dt As DataTable = New DataTable()
         dim dr As DataRow
 
         ' Define the columns of the table.
         dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
         dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
         dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
         dt.Columns.Add(New DataColumn("ImageValue", GetType(String)))
 
         ' Populate the table with sample values.
         Dim i As Integer

         For i = 0 To 8 

            dr = dt.NewRow()
 
            dr(0) = i
            dr(1) = "Description for item " & i.ToString()
            dr(2) = 1.23 * (i + 1)
            dr(3) = "Image" & i.ToString() & ".jpg"
 
            dt.Rows.Add(dr)

         Next i
 
         Dim dv As DataView = New DataView(dt)
         Return dv

      End Function
 
      Sub Page_Load(sender As Object, e As EventArgs) 

         ' Load sample data only once, when the page is first loaded.
         If Not IsPostBack Then 
     
            ItemsList.DataSource = CreateDataSource()
            ItemsList.DataBind()
         
         End If

      End Sub

      Sub Selection_Change(sender As Object, e As EventArgs)

         ' Set the background color for the heading and footer sections
         ' of the DataList control.
         ItemsList.ItemStyle.BackColor = _
             System.Drawing.Color.FromName(ItemList.SelectedItem.Value)
         ItemsList.AlternatingItemStyle.BackColor = _
             System.Drawing.Color.FromName(AltItemList.SelectedItem.Value)

      End Sub
 
   </script>
 
<head runat="server">
    <title>DataList ItemStyle and AlternatingItemStyle Example</title>
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>DataList ItemStyle and AlternatingItemStyle Example</h3>

      Select background colors for the items and alternating items.

      <br /><br />
 
      <asp:DataList id="ItemsList"
           BorderColor="black"
           CellPadding="5"
           CellSpacing="5"
           RepeatDirection="Vertical"
           RepeatLayout="Table"
           RepeatColumns="3"
           ShowFooter="True"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <ItemStyle BackColor="White">
         </ItemStyle>

         <AlternatingItemStyle BackColor="White">
         </AlternatingItemStyle>

         <HeaderTemplate>

            List of items

         </HeaderTemplate>
               
         <ItemTemplate>

            Description: <br />
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>

            <br />

            Price: <%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>

            <br />

            <asp:Image id="ProductImage"
                 AlternatingText='<%# DataBinder.Eval(Container.DataItem, "StringValue") %>'
                 ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
                 runat="server"/>

         </ItemTemplate>
 
      </asp:DataList>

      <hr />

      <table cellpadding="5">

         <tr>

            <td>

               Item BackColor:
 
            </td>

            <td>

               Alternating item BackColor:

            </td>

         </tr>

         <tr>

            <td>

               <asp:DropDownList id="ItemList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>
 
            </td>

            <td>

               <asp:DropDownList id="AltItemList"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="Selection_Change"
                    runat="server">

                  <asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
                  <asp:ListItem Value="Silver"> Silver </asp:ListItem>
                  <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
                  <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
                  <asp:ListItem Value="DarkKhaki"> Dark Khaki </asp:ListItem>

               </asp:DropDownList>

            </td>

         </tr>

      </table>
 
   </form>
 
</body>
</html>

Comentarios

Utilice esta propiedad para proporcionar un estilo personalizado para los elementos del DataList control. Los atributos de estilo comunes que se pueden ajustar incluyen el color de primer plano, el color de fondo, la fuente y la alineación del contenido dentro de la celda. Proporcionar un estilo diferente mejora la apariencia del DataList control.

Las propiedades de estilo de elemento del DataList control se heredan de una propiedad de estilo de elemento a otra a través de una jerarquía. Las propiedades de estilo de elemento establecidas en la jerarquía se heredan por propiedades de estilo de elemento superiores en la jerarquía. Por ejemplo, si especifica una fuente roja para la ItemStyle propiedad , todas las demás propiedades de estilo de elemento del DataList control también tendrán una fuente roja. Esto le permite proporcionar una apariencia común para el control estableciendo una propiedad de estilo de elemento único. Puede invalidar la configuración de estilo heredada para una propiedad de estilo de elemento que sea superior en la jerarquía estableciendo sus propiedades de estilo. Por ejemplo, puede especificar una fuente azul para la AlternatingItemStyle propiedad , reemplazando la fuente roja especificada en la ItemStyle propiedad . En la tabla siguiente se muestra el orden de jerarquía de mayor a menor.

Prioridad Style (propiedad)
1 EditItemStyle
2 SelectedItemStyle
3 AlternatingItemStyle
4 ItemStyle
5 ControlStyle

Para especificar un estilo personalizado para los elementos del DataList control, coloque las <ItemStyle> etiquetas entre las etiquetas de apertura y cierre del DataList control. A continuación, puede enumerar los atributos de estilo dentro de la etiqueta de apertura <ItemStyle> .

También puede usar la AlternatingItemStyle propiedad para proporcionar una apariencia diferente para los elementos alternativos en el DataList control.

Se aplica a

Consulte también