| |||||
Introduction to Data Binding in Web FormsASP.NET Web Forms Data Binding in Web Forms Web Forms enable the display of dynamic content by providing a simple yet flexible means of binding controls to a data source. Data binding in Web Forms, though, works somewhat differently than it does in traditional forms-based applcations, such as Windows desktop applications. This is mainly due to the nature of HTTP, a Web standard that governs how a Web server responds to a Web browser request in general. This document covers in brief an overview of the ASP.NET data-binding model, and how data binding can effectively be used in ASP.NET Web applications. Data Binding OverviewThe data-binding architecture of Web Forms pages is based on the presumption that data access in the vast majority of Web applications is read-only that there is much more often need to display information from a data store, than there is to write data back to the store. Typical examples of applications that present read-only data are
Because the Web Forms data-binding architecture does not perform updates that is, it does not write data from the control back to the data source you must provide this logic. The logic for performing updates in Web Forms pages varies considerably according to a variety of factors:
Because of the variables associated with updating from Web Forms pages, you implement your own update logic to match the specific requirements of your Web Forms page and application. Binding PropertiesData binding in Web Forms pages is flexible you can bind any property of a control to data. In contrast, in traditional data binding, you typically bind the display property ( for example, the text property of a text box ) to the data source. In Web Forms pages, your data source can be almost any type of information available to the page whether it comes from a database, from an XML document, from another control or process, or even from a calculation you perform yourself. Web Forms data binding, moreover, allows you to assign this data to any property of any control. Because of this, you can use data binding to perform actions such as:
In other words, data binding becomes an automatic way of setting any run-time accessible property of any control on the page. Binding Multi-Record and Single-Value ControlsThere are two types of data-binding that a control might support:
Data binding works differently depending on whether you are binding to multiple records or to a single data value. To display multiple records, you bind a multi-record control to a data source that contains multiple records an array, a collection, a data table, and so on. The only requirement is that the data source implement the IEnumerable interface. The default behavior is that when data binding occurs, the multi-record control loops through every data item in the data source. To bind to a single value, on the other hand, you specify what property to bind and what data value it should get its value from. For more information about how data binding works in each of these types of controls, see Multi-Record and Single-Value Data Binding. Binding at Design Time vs. at Run TimeIf you are binding controls to data in a dataset, you can specify data-binding properties for controls at design time. This includes binding to dataset tables and to data views. For example, you can use design-time tools to specify that a DataGrid control be bound to a table in a dataset. For more information, see Data Binding Multi-Record Web Server Controls. Similarly, you can bind a TextBox control to a value in a row returned by a data view. Visual Studio® includes tools to help you easily specify this kind of design-time binding. For details, see Data Binding Single-Value Web Server Controls at Design Time. In some situations, however, it is impractical to specify binding properties at design time. Generally, if the data source you want to bind to does not have an instance on your Web Forms page at design time, you need to perform data binding at run time. Some examples where you might do this include:
At run time, controls raise a DataBinding event that you can use to fetch data values and assign them to control properties. |
| ||||
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme