CreateControlStyle Method

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

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2016 Microsoft