DataList.CreateControlStyle Method ()


Creates the default style object that is used internally by the DataList control to implement all style related properties.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

protected override Style CreateControlStyle()

Return Value

Type: System.Web.UI.WebControls.Style

A TableStyle that contains the default style properties for the control.

The CreateControlStyle method is used primarily by control developers in deriving a custom implementation from the DataList control.

The following code example demonstrates how to override the CreateControlStyle method in a custom server control so that it always displays horizontal grid lines with no cell spacing in the DataList control.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
    <title>Custom DataList - CreateControlStyle - C# Example</title>
    <script runat="server">
      private void Page_Load(object sender, System.EventArgs e)
        // Create sample data for the DataList control.
        System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("Column1", typeof(String)));

        dr = dt.NewRow();
        dr[0] = "Hello";

        dr = dt.NewRow();
        dr[0] = "DataList";

        dr = dt.NewRow();
        dr[0] = "World";

        // Show the DataTable values in the DataList.
        DataList1.DataSource = dt;
    <form id="Form1" method="post" runat="server">
      <h3>Custom DataList - CreateControlStyle - C# Example</h3>

      <aspSample:CustomDataListCreateControlStyle id="DataList1" runat="server" BorderColor="#999999" BorderStyle="None" BackColor="White" CellPadding="3" GridLines="Vertical" BorderWidth="1px" Width="100px">

        <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084" />
          <asp:Label id="Label1" runat="server">Column1</asp:Label>

        <ItemStyle ForeColor="Black" BackColor="#EEEEEE" />
          <asp:Label id="Label2" runat="server"><%# DataBinder.Eval(Container.DataItem, "Column1") %></asp:Label>

        <AlternatingItemStyle BackColor="#DCDCDC" />
          <asp:Label id="Label3" runat="server"><%# DataBinder.Eval(Container.DataItem, "Column1") %></asp:Label>


using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class CustomDataListCreateControlStyle : System.Web.UI.WebControls.DataList
        protected override System.Web.UI.WebControls.Style CreateControlStyle()
            // Create a new TableStyle instance based on ViewState values.
            System.Web.UI.WebControls.TableStyle style = new System.Web.UI.WebControls.TableStyle(ViewState);

            // Show the GridLines horizontal with no CellSpacing.
            style.GridLines = System.Web.UI.WebControls.GridLines.Horizontal;
            style.CellSpacing = 0;

            // Return the Style
            return style;

.NET Framework
Available since 1.1
Return to top