Select the product you need help with
ASP.NET data binding overviewArticle ID: 307860 - View products that this article applies to. This article was previously published under Q307860 This article refers to the following Microsoft .NET
Framework Class Library namespaces:
On This PageSummary This article provides an introduction to ASP.NET data
binding. For additional ASP.NET overviews, see the following Microsoft Knowledge Base article: 305140
(http://support.microsoft.com/kb/305140/
)
ASP.NET roadmap
More information With ASP.NET data binding, you can bind any server control
to simple properties, collections, expressions and/or methods. When you use
data binding, you have more flexibility when you use data from a database or
other means. This article addresses the following data binding topics: Data binding essentials<%# %> SyntaxASP.NET introduces a new declarative syntax, <%# %>. This syntax is the basis for using data binding in an .aspx page. All data binding expressions must be contained within these characters. The following list includes examples of simple data binding from multiple sources:
Data Binding Expression Syntax http://msdn2.microsoft.com/en-us/library/bda9bbfx(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/bda9bbfx(vs.71).aspx)
Page.DataBind() versus Control.DataBind()After the particular data sources have been determined and set for the objects on the .aspx page, you must bind the data to these data sources. You can use the Page.DataBind or the Control.DataBind method to bind the data to the data sources.Both methods work similarly. The main difference is that all data sources are bound to their server controls after the Page.DataBind method is called. No data is rendered to the control until you explicitly call either the DataBind method of the Web server control or until you invoke the page-level Page.DataBind method. Typically, Page.DataBind (or DataBind) is called from the Page_Load event. For more information about the DataBind method, see the following .NET Framework SDK documentation: Control.DataBind Method http://msdn.microsoft.com/en-us/library/w5e5992d.aspx
(http://msdn.microsoft.com/en-us/library/w5e5992d.aspx)
Data-bound list controlsThe list controls are special Web server controls that can bind to collections. You can use these controls to display rows of data in a customized template format. All list controls expose the DataSource and the DataMember properties, which are used to bind to collections.These controls can bind their DataSource property to any collection that supports the IEnumerable, the ICollection, or the IListSource interface. Repeater controlThe Repeater control is a templated, data-bound list. The Repeater control is "lookless;" that is, it does not have any built-in layout or styles. Therefore, you must explicitly declare all HTML layout, formatting, and style tags in the control's templates.The following code samples demonstrate how you can use one list control, the Repeater control, to display data: NOTE: You must modify the parameters of the connection string as necessary for your environment. Visual Basic .NET
Repeater Web Server Control http://msdn.microsoft.com/en-us/library/x8f2zez5.aspx
(http://msdn.microsoft.com/en-us/library/x8f2zez5.aspx)
DataList controlThe DataList class is a feature-rich, templated, data-bound list. You can modify the templates to customize this control. Unlike the Repeater control, DataList supports directional rendering and can optionally render in an HTML table at run time.For more information about the DataList control, see the following .NET Framework SDK documentation:
DataList Web Server Control http://msdn.microsoft.com/en-us/library/9cx2f3ks(VS.85).aspx
(http://msdn.microsoft.com/en-us/library/9cx2f3ks(VS.85).aspx)
DataGrid controlThe DataGrid control is a fully featured, multicolumn, data-bound grid. To customize the layout of individual columns in the DataGrid, you can set the column type to "templated" and modify the column's templates. The DataGrid control can render without templates, which makes this control ideal for reporting scenarios. DataGrid also supports selection, editing, deletion, paging, and sorting by column and button columns.For more information about the DataGrid control, see the following .NET Framework SDK documentation:
DataGrid Web Server Control http://msdn.microsoft.com/en-us/library/aa710742(VS.71).aspx
(http://msdn.microsoft.com/en-us/library/aa710742(VS.71).aspx)
Accessing dataThis section describes how to access data from a database and bind the data to list controls. You can use the DataSet or the DataReader class to obtain data from a database.DataSet classA DataSet contains a complete representation of data, including the table structure, the relationships between tables, and the ordering of the data. DataSet classes are flexible enough to store any kind of information from a database to an Extensible Markup Language (XML) file. DataSet classes are stateless; that is, you can pass these classes from client to server without tying up server connection resources. The following code demonstrates how to use a DataSet to bind data to a control:NOTE: You must modify the parameters of the connection string as necessary for your environment. Visual Basic .NET
DataSet Class http://msdn2.microsoft.com/en-us/library/system.data.dataset(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/system.data.dataset(vs.71).aspx)
DataReader classConversely, if you only need to display (and not change) the data that is to be rendered, a DataReader class may be a better solution. For example, it is better to use a DataReader for a DropDownList control because the DataReader is a forward-only data cursor. The following code demonstrates how to use a SqlDataReader class to bind data to a control:Visual Basic .NET
SqlDataReader Class http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx
(http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx)
Developing High-Performance ASP.NET Applications http://msdn2.microsoft.com/en-us/library/5dws599a(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/5dws599a(vs.71).aspx)
Binding in list control templatesYou can use templates in the list controls to bind and to customize individual records of a data source. This section includes three methods to do this.DataBinder.Eval methodWhen the data source works with data that is returned from a database, the data source may contain numerous pieces of information. You can use the generic DataBinder.Eval method to return data. In the following code sample, the "au_id" field is returned from the data source of the container object:
DataBinder.Eval Method http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx
(http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx)
Explicit castingIf you need more control, use explicit casting. An explicit conversion uses a type conversion keyword. These keywords act as functions, but the compiler generates the code inline. Therefore, execution is slightly faster than with a function call. The following code samples use explicit casting:Visual Basic .NET ItemDataBound eventYou can also use the ItemDataBound event of the control to bind the data. This event occurs when an item is data bound to the control. The following HTML code sample defines a Repeater control with an ItemTemplate:Visual Basic .NET PropertiesArticle ID: 307860 - Last Review: October 15, 2012 - Revision: 5.0 Applies to
| Article Translations
|


Back to the top








