Asp.net| Create | dynamic
Summary: The DataGrid control is a data-bound server control that is distributed with the ASP.net Page Framework. This article uses the DataGrid to build a Web page that contains dynamic table views. It also explores various aspects of the functionality that the control provides, including selection, deletion, paging, and template columns, which are used by the control to build the final page.
Brief introduction
The DataGrid control can be used with several read-only data. The control can be used to simplify the output of a data table layout. It also provides several mechanisms for adding interactivity to the output through hyperlinks and their support for selection, sorting, paging, and in-place editing and other features. This makes the control useful in a number of common WEB application scenarios, such as lists, shopping carts, and query results.
The DataGrid also provides features that feature all server controls that are unique to the ASP.net architecture. The control contains the logic required for browser-independent output, while providing a unified programming model to handle the return data and manage the state between requests. In this way, developers can program on object models with attributes, methods, and events without having to deal with inconsistencies and complexities that are directly associated with HTML programming.
What are we going to build?
This article cites a series of sample pages that combine to eventually generate a page that provides a master/detail view (shipped with Microsoft SQL server?2000) based on the Authors table and the Titles table of the example database. Each page in the sequence describes a new feature or feature of the DataGrid control. The following figure is extracted from the pubs database.
The master/detail view resembles the form/subform concept that Microsoft Access describes. Also similar to the DataForm Wizard (Data Form Wizard) published with Microsoft Visual InterDev 6.0. The Master/Detail view displays one or more relationship results, where one part of the view displays the results of the first query or the main query. A selection is then tracked to filter the results of the second query used, displaying details of the selection in another part of the view.
Figure 1. Finished page
Figure 1 Displays the Author list in the top half of the page and displays detailed information about the selected author (including related titles) in the lower part. Authors lists and Titles are represented by the DataGrid control. The DataGrid that displays the author illustrates how to select, Sort, and page out. The DataGrid showing the title shows how to edit, format, and customize columns in situ.
Data access
To make the example self-contained, extract the data from SQL Server and leave the data together with its schema information as an XML file titlesdb.xml. The following is a fragment of the file.
<root>
<schema id= "documentelement" targetnamespace= ""
Xmlns= "Http://www.w3.org/1999/XMLSchema"
Xmlns:msdata= "Urn:schemas-microsoft-com:xml-msdata" >
<element name= "Author"
<complextype content= "Elementonly"
<element name= "au_id" type= "string" minoccurs= "1"
maxoccurs= "1" > </element>
<element name= "Au_name" type= "string" minoccurs= "1"
maxoccurs= "1" > </element>
<element name= "Address" type= "string" minoccurs= "0"
maxoccurs= "1" > </element>
<element name= "City" type= "string" minoccurs= "0"
maxoccurs= "1" > </element>
<element name= "state" type= "string" minoccurs= "0"
maxoccurs= "1" > </element>
<element name= "Zip" type= "string" minoccurs= "0"
maxoccurs= "1" > </element>
<element name= "Phone" type= "string" minoccurs= "0"
maxoccurs= "1" > </element>
</complexType>
<unique name= "Authorconstraint" msdata:primarykey= "True"
<selector>. </selector>
<field> au_id </field>
</unique>
</element>
<element name= "Title" >
<complextype content= "Elementonly"
<element name= "title_id" type= "string" minoccurs= "1"
maxoccurs= "1" > </element>
<element name= "au_id" type= "string" minoccurs= "1"
maxoccurs= "1" > </element>
<element name= "title" Type= "string" minoccurs= "1"
maxoccurs= "1" > </element>
<element name= "Price" msdata:datatype= "System.currency"
Type= "string"
minoccurs= "1" maxoccurs= "1" > </element>
<element name= "pubdate" type= "Timeinstant" minoccurs= "1"
maxoccurs= "1" > </element>
</complexType>
<unique name= "Titleconstraint" msdata:primarykey= "True"
<selector>. </selector>
<field> title_id </field>
</unique>
<key name= "AuthorTitle"
<selector>.. /author </selector>
<field> au_id </field>
</key>
<keyref refer= "AuthorTitle"
<selector>. </selector>
<field> au_id </field>
</keyref>
</element>
</schema>
<DocumentElement>
<Author>
<au_id> 154-00-1300 </au_id>
<au_name> John Doe </au_name>
<phone> 425 705 1234 </phone>
<address> One Microsoft Way </address>
<city> Redmond </city>
<state> CA </state>
<zip> 98005 </zip>
</Author>
<Title>
<title_id> BU1032 </title_id>
<au_id> 213-46-8915 </au_id>
<title> the Busy Executive ' s Database Guide </title>
<price> 19.99 </price>
<pubdate> 1991-06-12t07:00:00 </pubdate>
</Title>
</DocumentElement>
</root>
These samples simplify data access and focus all on the use of the DataGrid. The XML above is loaded into a DataSet. Datasets provide caching for data, allowing you to filter, sort, edit, and so on. The following code comes from Global.asax, which loads the DataSet and saves it as a session state.
public void Session_OnStart () {
The dataset used in the sample is loaded into the scope of the membership session.
FileStream fs = null;
DataSet ds = null;
try {
FS = new FileStream (Server.MapPath ("Data\\titlesdb.xml"),
FileMode.Open, FileAccess.Read);
ds = new DataSet ();
Ds. READXML (FS);
finally {
if (fs!= null) {
Fs. Close ();
FS = null;
}
}
session["AppData"] = ds;
}
[1] [2] [3] [4] [5] [6] Next page