DataList.CreateControlStyle Method
.NET Framework 3.0
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)
Assembly: System.Web (in system.web.dll)
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.JSL.Controls" Assembly="Samples.AspNet.JSL" %>
<%@ Page language="VJ#" %>
<!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 - 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
Community Additions
ADD
Show: