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 Overrides Function CreateControlStyle As Style

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.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page language="vb" %>
<!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" >
  <head>
    <title>Custom DataList - CreateControlStyle - VB Example</title>
    <script runat="server">
      Private Sub Page_Load(sender As Object, e As EventArgs)

        ' Create sample data for the DataList control.
        Dim dt As System.Data.DataTable = New System.Data.DataTable()
        Dim dr As System.Data.DataRow

        ' Create a new column named Column1, of type String.
        Dim col As New System.Data.DataColumn("Column1", GetType(String))
        ' Add the column to the DataTable.
        dt.Columns.Add(col)

        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()

      End Sub ' Page_Load
    </script>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <h3>Custom DataList - CreateControlStyle - VB 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>
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomDataListCreateControlStyle
        Inherits System.Web.UI.WebControls.DataList

        Protected Overrides Function CreateControlStyle() As System.Web.UI.WebControls.Style

            ' Create a new TableStyle instance based on ViewState values.
            Dim style As 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
        End Function
    End Class
End Namespace

.NET Framework
Available since 1.1
Return to top
Show: