DataBinder Class (System.Web.UI)

Switch View :
ScriptFree
.NET Framework Class Library
DataBinder Class

Provides support for rapid application development (RAD) designers to generate and parse data-binding expression syntax. This class cannot be inherited.

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

Visual Basic (Declaration)
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class DataBinder
Visual Basic (Usage)
Dim instance As DataBinder
C#
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class DataBinder
Visual C++
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class DataBinder sealed
JScript
public final class DataBinder
Remarks

You can use the overloaded static Eval method of this class in data-binding syntax in an ASP.NET Web page. This provides an easier syntax to work with than standard data binding. However, because DataBinder.Eval provides automatic type conversion, it can result in slower performance.

For more information about ASP.NET data binding, expressions, and syntax, see Binding to Databases and Data-Binding Expressions Overview.

Examples

The following example uses the static GetPropertyValue method to populate the fields of a Repeater control using an ArrayList of Product objects. The Eval method could be applied with the same syntax, but it would not perform as quickly.

This example assumes the existence of a custom class which exposes a string Model property and a numeric UnitPrice property.

Visual Basic
<%@ Page Language="vb" %>
<%@ Import Namespace="ASPSample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        ' Create and populate an ArrayList to store the products.
        Dim ProductList As New ArrayList
        ProductList.Add(New Product("Standard", 99.95))
        ProductList.Add(New Product("Deluxe", 159.95))

        ' Bind the array list to Repeater
        ListRepeater.DataSource = ProductList
        ListRepeater.DataBind()

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>DataBinder Example</title>
</head>
<body>
<form id="Form2" runat="server">
<table>
<asp:Repeater id="ListRepeater" runat="server">
    <HeaderTemplate>
	<tr>
		<th style="width:50; text-align:left">Model</th>
		<th style="width:100; text-align:right">Unit Price</th>
	</tr>
	</HeaderTemplate>
	<ItemTemplate>
    <tr>
        <!-- Databind to the Product information using the DataBinder methods. 
             The Container.DataItem refers to the ArrayList object bound to 
             the ASP:Repeater in the Page Load event. -->
	    <td>
	        <%#DataBinder.GetPropertyValue(Container.DataItem, "Model")%>
	    </td>
	    <!-- Format the UnitPrice as currency. ({0:c}) -->
	    <td style="text-align:right">
	        <%#DataBinder.GetPropertyValue(Container.DataItem, _
	                     "UnitPrice", "{0:c}")%>
	    </td>
    </tr>
	</ItemTemplate>
</asp:Repeater>
</table>
</form>
</body>
</html>


C#
<%@ Page Language="C#" %>
<%@ Import Namespace="ASPSample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void  Page_Load(object sender, EventArgs e)
{
        // Create and populate an ArrayList to store the products.
        ArrayList ProductList = new ArrayList();
        ProductList.Add(new Product("Standard", 99.95));
        ProductList.Add(new Product("Deluxe", 159.95));

        // Bind the array list to Repeater
        ListRepeater.DataSource = ProductList;
        ListRepeater.DataBind();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>DataBinder Example</title>
</head>
<body>
<form id="Form2" runat="server">
<table>
<asp:Repeater id="ListRepeater" runat="server">
    <HeaderTemplate>
	<tr>
		<th style="width:50; text-align:left">Model</th>
		<th style="width:100; text-align:right">Unit Price</th>
	</tr>
	</HeaderTemplate>
	<ItemTemplate>
    <tr>
        <!-- Databind to the Product information using the DataBinder methods. 
             The Container.DataItem refers to the ArrayList object bound to 
             the ASP:Repeater in the Page Load event. -->
	    <td>
	        <%#DataBinder.GetPropertyValue(Container.DataItem, "Model")%>
	    </td>
	    <!-- Format the UnitPrice as currency. ({0:c}) -->
	    <td style="text-align:right">
	        <%#DataBinder.GetPropertyValue(Container.DataItem,
	                     "UnitPrice", "{0:c}")%>
	    </td>
    </tr>
	</ItemTemplate>
</asp:Repeater>
</table>
</form>
</body>
</html>


The following code is the custom Product class. This code should be included in a separate class file in the App_Code directory, such as Product.cs or Product.vb.

Visual Basic
Namespace ASPSample

    Public Class Product
        Private _Model As String
        Private _UnitPrice As Double

        ' The product Model.
        Public Property Model() As String
            Get
                Return _Model
            End Get
            Set(ByVal Value As String)
                _Model = Value
            End Set
        End Property

        ' The price of the each product.
        Public Property UnitPrice() As Double
            Get
                Return _UnitPrice
            End Get
            Set(ByVal Value As Double)
                _UnitPrice = Value
            End Set
        End Property


        Public Sub New(ByVal Model As String, ByVal UnitPrice As Double)
            _Model = Model
            _UnitPrice = UnitPrice
        End Sub

    End Class

End Namespace


C#
namespace ASPSample
{

    public class Product
    {
        string _Model;
        double _UnitPrice;

        public Product(string Model, double UnitPrice)
        {
            _Model = Model;
            _UnitPrice = UnitPrice;
        }

        // The product Model.
        public string Model
        {
            get {return _Model;}
            set {_Model = value;}
        }

        // The price of the each product.
        public double UnitPrice
        {
            get {return _UnitPrice;}
            set {_UnitPrice = value;}
        }
    }
}


.NET Framework Security

Inheritance Hierarchy

System.Object
  System.Web.UI.DataBinder
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

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.
Version Information

.NET Framework

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

Reference

Other Resources

Community Content

Bart Verkoeijen1
No example
This page doesn't have the source code of the example.