RenderContents Method

DataList.RenderContents Method

Renders the list items in the DataList control.

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

protected internal override void RenderContents(
	HtmlTextWriter writer


Type: System.Web.UI.HtmlTextWriter

A HtmlTextWriter that represents the output stream to render HTML content on the client.

The RenderContents method is used primarily by control developers, when deriving a custom control from the DataList control.

The RenderContents method renders the inner content of the DataList control, including the contained DataListItem controls.

The following code example demonstrates how to override the RenderContents method in a custom server control so that some text precedes the DataList object.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
        <title>Custom DataList - RenderContents - 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 - RenderContents - C# Example</h3>

            <aspSample:CustomDataListRenderContents 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
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class CustomDataListRenderContents : System.Web.UI.WebControls.DataList
        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
            // Place some text before the DataList.
            writer.Write("Here is some text from the RenderContent method.<br>");

            // Call the base RenderContents method.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

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

Community Additions

© 2016 Microsoft