Export (0) Print
Expand All

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 ()
protected Style CreateControlStyle ()
protected override function CreateControlStyle () : Style

Return Value

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#" %>
<HTML>
  <HEAD>
    <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";
        dt.Rows.Add(dr);

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

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

        // Show the DataTable values in the DataList.
        DataList1.DataSource = dt;
        DataList1.DataBind();
    }
    </script>
  </HEAD>
  <body>
    <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" />
        <HeaderTemplate>
          <asp:Label id="Label1" runat="server">Column1</asp:Label>
        </HeaderTemplate>

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

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

      </aspSample:CustomDataListCreateControlStyle>

    </form>
  </body>
</HTML>
...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;
        }
    }
}

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL.Controls" Assembly="Samples.AspNet.JSL" %>
<%@ Page language="VJ#" %>
<HTML>
  <HEAD>
    <title>Custom DataList - CreateControlStyle - VJ# 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.get_Columns().Add(new System.Data.DataColumn("Column1", 
            String.class.ToType()));

        dr = dt.NewRow();
        dr.set_Item(0, "Hello");
        dt.get_Rows().Add(dr);

        dr = dt.NewRow();
        dr.set_Item(0, "DataList");
        dt.get_Rows().Add(dr);

        dr = dt.NewRow();
        dr.set_Item(0, "World");
        dt.get_Rows().Add(dr);

        // Show the DataTable values in the DataList.
        DataList1.set_DataSource(dt);
        DataList1.DataBind();
    } //Page_Load
    </script>
  </HEAD>
  <body>
    <form id="Form1" method="post" runat="server">
      <h3>Custom DataList - CreateControlStyle - VJ# 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" />
        <HeaderTemplate>
          <asp:Label id="Label1" runat="server">Column1</asp:Label>
        </HeaderTemplate>

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

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

      </aspSample:CustomDataListCreateControlStyle>

    </form>
  </body>
</HTML>
...package Samples.AspNet.JSL.Controls;

public class CustomDataListCreateControlStyle
    extends System.Web.UI.WebControls.DataList
{
    protected 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(get_ViewState());
        // Show the GridLines horizontal with no CellSpacing.
        style.set_GridLines(System.Web.UI.WebControls.GridLines.Horizontal);
        style.set_CellSpacing(0);
        // Return the Style
        return style;
    } //CreateControlStyle
} //CustomDataListCreateControlStyle

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

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft