Skip to main content

ASP.NET Working With Data-Bound Web Server Controls

Suppose we have:
List<Car> vCars = new List<Car>();

There are three types of databound controls: Simple databound controls(List, AdRotater), Composite data bound controls(GridView, DetailsView, FormView that inherit from CompositeDataBoundControl), and Hierarchal data bound controls (TreeView, Menu).

 



DataBoundControl has a DataBind method that can be used when data is ready. It calls DataBind for child controls as well. Page.DataBind() will call DataBind for all child controls.

Using DataSource Objects:
                                      BaseDataBound control exposes DataSource property that accepts objects that implement IEnumerable, IListSource, IDataSource, or IHierarchalDataSource. DataSourceID accepts ID of SqlDataSource. If both specified DataSourceID takes precedence.
GUI based data sources DataSourceControl or HierarchalDataSourceControl implement IDataSource and IListSource interface.
 

GUI Based Data Source
Description
AccessDataSource
MS Access (.mdb)
SqlDataSource
Open Database Connectivity(ODBC), Object Linking and Embedding Databases(OLEDB), SQL Server, SQL (.mdf)
XmlDataSource
XML file within your project. Use XPath for a subset, or XSL Transform can also be used.
ObjectDataSource
Any object implementing IEnumerable, IListSource, IDataSource, IHierarchalDatasource can be used. Like a collection, DataSet, DataTable can be used. 
SitemapDataSource
A valid sitemap file at root of the application.

Mapping Fields to Templates:
                                                Only controls that support templates. Templates have no default interface. Controls only provide binding support. Developer provides UI for templates. Templates can also contain ASP.NET Binding commands. Controls that support templates are GridView, DetailsView, and FormView.

Template
Description
HeaderTemplate
Optional. Header at the top of control.
FooterTemplate
Optional. Footer at the bottom of the control.
ItemTemplate
ItemTemplate is rendered for each row of the DataSource.
AlternatingItemTemplate
Optional. Just like ItemTemplate, But for each odd index.
SelectedItemTemplate
Optional. Template for Seleted Item Template.
SepratorTemplate
Optional. Template to seprate Item and alternate item.
EditItemTemplate
Optional. Edit mode template.



Using the DataBinder Class:
                                                DataBinder class provides a static method called Eval to access Data. Eval uses reflection, looks for the item in DataItem and retrieves the value.

<#% Eval(“VIN”) %>
An overload also provides format capabilities.
<#% Eval(“VIN”, “{0:C}”) %> //Currency

Eval provides read only access.
ASP.Net 2.0 provides Bind that allows updates as well.
<#% Bind(“VIN”) %>
<#% Bind(“VIN”, “{0:C}”) %>
GridView, DetailsView, and FormView only support Bind. The control must have a user-defines ID set, in order to user Bind.

Comments

Popular posts from this blog

Culture Information and Localization in .NET

Namespace: System.Globalization CultureInfo Class:                 It provides information like the Format of numbers and dates, Culture’s Calendar, Culture’s language and sublanguage (if applicable), Country and region of the culture. The Basic use of CultureInfo class is shown here: • How string Comparisons are performed • How Number Comparison & Formats are performed • Date Comparison and Formats. • How resources are retrieved and used. Cultures are grouped into three categories: Invariant Culture : It’s Culture Insensitive. It can be used to build some trial application. It can be also used to build an application with hard-coded expiry date that ignores cultures. But using it for every comparison will be incorrect and inappropriate. Neutral Culture : English(en), Frensh(fr), and Spanish(sp). A neutral culture is related to language but it’s not related to specific regi...

Data Serialization in .Net

Namespace: System.Runtime.Serialization Serialization is the process of converting object so that they can be transmitted over the network, or stored in file for later use, or passing them to a web service or any other application. .Net WebServices, Session Object, .Net Remoting and clipboard rely on Serialization. Binary Serialization: It’s efficient but not portable solution. String data = “Hello”; FileStream fs = new FileStream(“FileStream.Data”, FileMode.Create); BinaryFormatter bf = new BinarFormatter (); Bf.Serilize(fs, data); //Wrting Objects Bf.Serilize(fs,System.DateTime.Now); Fs.close(); Deserializing Objects FileStream fs = new FileStream(“FileStream.Data”, FileMode.Open); BinaryFormatter bf = new BinarFormatter (); String Data = (String)bf.Deserialize(fs); DateTime vTime = (DateTime) bf.Derialize(fs); To serialize a class one must the Serializeable attribute to that class. To omit some attribute from deseriali...

Using ADO.NET Transaction Object, Saving CLR Objects and SqlNotificationRequest

Serializable allows one transaction to complete before the other start. SqlConnectoin support named savpoint to roll back to; that’s an equaliant to save transaction command in MS SQL Server. Using TransactionScope Object : A transaction can’t span multiple connections.   Local and distributed transaction ares supported in 1.x you have to Enterpeise seveices library to regiser multiple connections and then call EnlistDistributedTransaction. Distributed Transaction Coordinator is required for Distributed transaction that’s available on windows 2000 & +. In 2.X and later use TransactionScope object. Serializable allows one transaction to complete before the other start. SqlConnectoin support named savpoint to roll back to; that’s an equaliant to save transaction command in MS SQL Server. Using TransactionScope Object : Dispoase of the transaction must be called to complete the transaction. Distributed Transaction : ...