Use the ASP + DATAGRID control to create a main viewdetail view

xiaoxiao2021-03-06  104

Nikhil Kothari

Microsoft Corporation

August 2000

Introduction

The next release of Microsoft® Visual Studio.NET includes a DataGrid Web Control (as part of the Active Server Page (ASP ) suite of the server control). This control provides a function of using the content of the data source to represent the HTML.

DataGrid controls can be used for several read-only reporting situations. This control is designed to simplify the output of rich and fully customizable data table layout. Multiple mechanisms are also available to add interactivity to the output via hyperlink and its support for selection, sort, paging, and originally editing and other characteristics. This makes this control in a number of common web applications, such as lists, shopping carts, and query results.

DataGrid also provides features that have the characteristics of all server controls unique to the ASP architecture. The control includes logic required to perform an output independent of the browser while providing a unified programming model to handle the return data, and managing the status between the request. In this way, developers can program for object models with attributes, methods, and events without having to handle inconsistency and complexity brought directly with HTML programming.

Use the ASP list binding control to introduce the DataGrid control and the relevant DataList and RePeater controls. It also introduces multiple key concepts for data binding, template, and formatting. This article is written based on the basis and focus on the DataGrid control to reveal how to use the functions provided by this control in your own application.

What should we build?

This article raises a sequence of example pages, combined with each other, which eventually generates a page, which is based on the Authors table and Titles table of the sample database, providing the main / detail view (this database along with Microsoft SQL ServerTM 2000) Shipment). Each page in the sequence introduces a new feature or feature of the DataGrid control. The following images are extracted from the Pubs database.

Master / Detail View similar to the form / sub-form concept described in Microsoft Access. It is also similar to the DataForm Wizard shipped together with Microsoft Visual InterDev® 6.0 (Data Form Wizard). The main / detailed view shows a multi-relation result, where a portion of the view displays the results of the first query or primary query. A selection is then tracked to filter the result of the second query used, so that the details of the selection content are displayed on another part of the view.

Figure 1. Completed page

Figure 1 displays the AUTHOR list in the upper part of the page, and displaying the details of the selected author (including the relevant book name) in the lower part. Authors list and titles are represented by DataGrid controls. The DataGrid example shows how to choose, sort, and pagination. The DataGrid showing the book name How to perform originally edited, format, and custom columns.

data access

In order to make examples, data is extracted from SQL Server and retain the data along with its architecture information as an XML file titlesdb.xml. Below is a piece of this file.

XMLns = "http://www.w3.org/1999/xmlschema"

XMLns: MSData = "URN: Schemas-Microsoft-COM: XML-MSDATA">

Maxoccurs = "1">

Maxoccurs = "1">

Maxoccurs = "1">

Maxoccurs = "1">

Maxoccurs = "1">

Maxoccurs = "1">

Maxoccurs = "1">

.

AU_ID

Maxoccurs = "1">

Maxoccurs = "1">

Maxoccurs = "1">

Minoccurs = "1" maxoccurs = "1">

Maxoccurs = "1">

.

Title_id

../ Author

AU_ID

.

AU_ID

154-00-1300

john doe

425 705 1234

One Microsoft Way

redmond

CA

98005

</p> <p><title_id> bu1032 </ title_id></p> <p><au_id> 213-46-8915 </ au_id></p> <p><title> The busy executive's database guide </ title></p> <p><price> 19.99 </ price></p> <p><Pubdate> 1991-06-12T07: 00: 00 </ pubdate></p> <p></ Title></p> <p></ Documentelement></p> <p></ root></p> <p>These samples simplify data access, which will focus all on the use of DataGrid. The above XML is loaded into a DataSet. DataSet provides caches to data, so as can be performed, such as screening, sorting, and editing. The following code comes from Global.asax, which is used to load DataSet and save it as a SESSION state.</p> <p>PUBLIC VOID session_onstart () {</p> <p>// Use the data used in the sample to load the DataSet of the session.</p> <p>FILESTREAM FS = NULL;</p> <p>DataSet DS = NULL;</p> <p>Try {</p> <p>Fs = new filestream (server.mappath ("data // titlesdb.xml),</p> <p>Filemode.open, FileAccess.read;</p> <p>DS = new dataset ();</p> <p>DS.ReadXML (FS);</p> <p>} finally {</p> <p>IF (fs! = null) {</p> <p>fs.close ();</p> <p>FS = NULL;</p> <p>}</p> <p>}</p> <p>SESSION ["AppData"] = DS;</p> <p>}</p> <p>In actual web applications, it is usually not used to use cache data in the session or Application status, but to access and modify data by the stored procedure, intermediate layer business object, or by calling the Web Service. Whether you have access to data, you will find that you are still in the same way and interact with the object model of the control. Www.ibuyspy.com (English) website is a good application example of demonstrating these concepts.</p> <p>Step 1: A basic DataGrid</p> <p>The first step of the sequence shows a page, which contains a separate DataGrid control for display a list of read-only authors from the data source. The results are similar to the effect of using the ASP list binding control (English).</p> <p>Figure 2. Finish the page behind the first step</p> <p>DataGrid declaration comes from:</p> <p>Step1.aspx:</p> <p><ask: DataGrid ID = "authorsgrid" runat = "server"</p> <p>AutogenerateColumn = "false"</p> <p>Backcolor = "White"</p> <p>Borderwidth = "1px" borderstyle = "solid" bordercolor = "tan"</p> <p>Cellpadding = "2" cellspacing = "0"</p> <p>Font-name = "Verdana" font-size = "8pt"></p> <p><Property Name = "Column"></p> <p><asp: boundcolumn headertext = "id" datafield = "au_id"></p> <p><Property Name = "Headerstyle"></p> <p><asp: tableitemstyle width = "100px" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p><ask: boundcolumn headertext = "name" datafield = "au_name"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "150px" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p><asp: boundcolumn headertext = "state" datafield = "state"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "50px" /></p> <p></ Property></p> <p></ ask: BoundColumn> </ property></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle BackColor = "DARKRED" forcolor = "White"</p> <p>Font-bold = "true" /></p> <p></ Property></p> <p><Property Name = "ItemStyle"></p> <p><ask: tableItemStyle Forecolor = "DARKSLATEBLUE" /></p> <p></ Property></p> <p><property name = "alternatingItemStyle"></p> <p><ask: TableItemStyle BackColor = "geger" /></p> <p></ Property></p> <p></ ask: DataGrid></p> <p>The above code shows DataGrid, the various properties of the control have been declared. The DataGrid control is shared with other web controls such as Font, BackColor, ForeColor, and BorderWidth shared a set of common style properties. In addition, DataGrid provides properties such as Cellpadding only for tables. Finally, DataGrid provides additional style properties that affect each item and the representation of Headerstyle, ItemStyle, and AlternatingItemstyle. These style properties are used to create a rich and very charming data visual effect.</p> <p>DataGrid supports the function of automatically generating a column from its binding data source. However, in this example, the AutoGenerateColumns property has been set to false. It is therefore necessary to initialize the columns collection by means of the columns to be displayed. Thereby, the performance effect, such as the order and header, and the pattern corresponding to each column can be more controlled. The columns defined in this step are BoundColumns so that they can be bound to a separate field of the data source through its DataField property. As you can see in the later steps, DataGrid allows you to select a variety of types of columns.</p> <p>The following class contains the code that supports this page.</p> <p>Step1page.cs:</p> <p>Namespace Samples {</p> <p>...</p> <p>Public Class Step1page: Page {</p> <p>Protected DataGrid Authorsgrid;</p> <p>// Retrieve application data stored in session status</p> <p>Private dataset getsessionData () {</p> <p>Return (Dataset) session ["appdata"];</p> <p>}</p> <p>// Retrieve an Authors table</p> <p>PRIVATE ICOLLECTION GETAUTHORS () {</p> <p>DataSet DS = GetSessionData ();</p> <p>DataView DV = DS.TABLES ["Author"]. DefaultView;</p> <p>DV.ROWFILTY;</p> <p>Return DV;</p> <p>}</p> <p>// load the authors table in DataGrid</p> <p>Private void loadingauthorsgrid () {</p> <p>Icollection authors = getAuthors ();</p> <p>Authorsgrid.datasource = authors;</p> <p>Authorsgrid.databind ();</p> <p>}</p> <p>// Supercontrol OnLoad to load data during the first request</p> <p>Protected Override Void OnLoad (Eventargs E) {Base.onLoad (e);</p> <p>IF (! ispostback) {</p> <p>LoadAuthorsgrid ();</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p>This class beyond the OnLoad method (similar to implementing page_load), load the Author list as the DataGrid data source. Like other server controls, DataGrid implements an explicit data binding model, where the control only enumerates the value in the data source when the DATABIND method is called. Designed to design each control, you can fully control which point in time requires the data source and reduces the data accesses when there is no data to minimize, such as most round-trip processes. This will be proof in future steps.</p> <p>The code also demonstrates data retrieved from the session state, and the data is in this state during the session startup process.</p> <p>Step 2: Main / Detail View with Multi-Page</p> <p>One way to create the master / detail view is to use multiple pages. In this method, the detailed view of the selected value in the primary query is displayed on the secondary page. In information regarding the selected content, the secondary page is passed as a parameter in the URL request.</p> <p>Figure 3. Floating view of the second post, show the floating view of the details page</p> <p>Step2.aspx contains changes to the DataGrid so that it contains a column named DataGrid declaration.</p> <p>Step2.aspx:</p> <p><ask: DataGrid ID = "authorsgrid" runat = "server"</p> <p>AutogenerateColumn = "false"</p> <p>Backcolor = "White"</p> <p>Borderwidth = "1px" borderstyle = "solid" bordercolor = "tan"</p> <p>Cellpadding = "2" cellspacing = "0"</p> <p>Font-name = "Verdana" font-size = "8pt"></p> <p><Property Name = "Column"></p> <p><asp: boundcolumn headertext = "id" datafield = "au_id"></p> <p><Property Name = "Headerstyle"></p> <p><asp: tableitemstyle width = "100px" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p><ask: boundcolumn headertext = "name" datafield = "au_name"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "150px" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p><asp: boundcolumn headertext = "state" datafield = "state"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "50px" /></p> <p></ Property></p> <p></ asp: boundcolumn> <asp: hyperlinkcolumn text = "details" datanavigateURLFIELD = "au_id"</p> <p>DataNavigateURLFORMATSTRING = "Step2a.aspx? Authorid = {0}" /></p> <p></ Property></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle BackColor = "DARKRED" forcolor = "White"</p> <p>Font-bold = "true" /></p> <p></ Property></p> <p><Property Name = "ItemStyle"></p> <p><ask: tableItemStyle Forecolor = "DARKSLATEBLUE" /></p> <p></ Property></p> <p><property name = "alternatingItemStyle"></p> <p><ask: TableItemStyle BackColor = "geger" /></p> <p></ Property></p> <p></ ask: DataGrid></p> <p>DataGrid declaration is almost the same as the DataGrid statement in step 1. A single HyperLinkColumn has been added to the columns collection. HyperLinkColumn is used to create a navigable link in each row of DataGrid. This type of column allows its text and navigateURL properties to bind to data. In this way, the Text property is static, and navigateURL is the data binding to the author ID (by setting the DataNavigateURL property). In addition, specifying DataNavigateURLFORMATSTRING to create a URL of the Detail page with the Author ID as a parameter. Therefore, each line includes an additional column with a "detailed" hyperlink, and the link URL is based on the data associated with the line.</p> <p>HyperlinkColumn represents the first step in adding interaction with the only read-only data display.</p> <p>The code that supports this page (step2page.cs) is the same as step 1. Adding this column does not add any code, nor does it cause any code to change, so there is no listings.</p> <p>Step2a.aspx Implements the details page to show the details of a specific author.</p> <p>Step2a.aspx:</p> <p><asp: panel id = "detailspanel" runat = "server"></p> <p><table border = "0" cellspacing = "0" cellpadding = "2" width = "100%"</p> <p>Style = "Font-Family: Verdana; Font-size: 8pt"></p> <p><tr></p> <p><TD Width = "200"> <b> name: </ b> </ td></p> <p><td width = "100%"></p> <p><% # DataBinder.eval (CurrentAuthor, "AU_NAME")%></p> <p></ td></p> <p></ TR></p> <p><TR> <TD Width = "200"> <b> id: </ b> </ td></p> <p><td width = "100%"></p> <p><% # DataBinder.eval (CurrentAuthor, "AU_ID")%></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TD Width = "200" Valign = "TOP"> <b> address: </ b> </ td></p> <p><td width = "100%"></p> <p><% # DataBinder.eval (CurrentAuthor, "Address")%> <br></p> <p><% # DataBinder.eval (CurrentAuthor, "City")%>,</p> <p><% # DataBinder.eval (CurrentAuthor, "State")%></p> <p><% # DataBinder.eval (CurrentAuthor, "Zip")%></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><td width = "200"> <b> phone: <b> </ td></p> <p><td width = "100%"></p> <p><% # DataBinder.eval (CurrentAuthor, "Phone")%></p> <p></ td></p> <p></ TR></p> <p><tr></p> <p><TD COLSPAN = "2"> <b> Title (s) authored </ b> </ td></p> <p></ TR></p> <p><tr></p> <p><TD COLSPAN = "2"></p> <p><ask: DataGrid ID = "titlesgrid" runat = "server"</p> <p>DataSource = '<% # DataBinder.eval (CurrentAuthor, "Authortitle")%>'</p> <p>AutogenerateColumn = "false"</p> <p>Showfooter = "True"</p> <p>Backcolor = "White"</p> <p>Borderwidth = "1px" borderstyle = "solid" bordercolor = "tan"</p> <p>Cellpadding = "2" cellspacing = "0"</p> <p>Font-name = "Verdana" font-size = "8pt"</p> <p>OnItemcreated = "onItemcreatedTitlesGrid"></p> <p><Property Name = "Column"></p> <p><asp: boundcolumn headertext = "id" datafield = "title_id"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "100px" /> </ property></p> <p></ ask: BOUNDCOLUMN></p> <p><ask: boundcolumn headertext = "title" datafield = "title"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "250px" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p><asp: boundcolumn headertext = "public" datafield = "pubdate"</p> <p>DataFormatString = "{0: MMM YYYY}"></p> <p><Property Name = "Headerstyle"></p> <p><asp: tableitemstyle width = "100px" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p><asp: boundcolumn headertext = "price" datafield = "price"</p> <p>DataFormatString = "{0: C}"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "50px" /></p> <p></ Property></p> <p><Property Name = "ItemStyle"></p> <p><ask: TableItemStyle Horizontalalign = "Right" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p></ Property></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle BackColor = "DARKRED" forcolor = "White"</p> <p>Font-bold = "true" /></p> <p></ Property></p> <p><Property Name = "Footerstyle"></p> <p><asp: tableitemstyle backcolor = "tan" /></p> <p></ Property></p> <p><Property Name = "ItemStyle"></p> <p><ask: tableItemStyle Forecolor = "DARKSLATEBLUE" /></p> <p></ Property></p> <p><property name = "alternatingItemStyle"></p> <p><ask: TableItemStyle BackColor = "geger" /></p> <p></ Property></p> <p></ ask: DataGrid></p> <p></ td></p> <p></ TR></p> <p></ table></p> <p></ asp: panel></p> <p>This page contains several data binding expressions that use DataBinder.eval. These expressions extract each attribute of the CurrentAr property implemented in this page code. DataGrid is used to display the title compiled by the selected author. As in step 1, the DataGrid contains a definition of the columns to be displayed, and a style property setting for providing visual formatting for columns, rows, and overall controls.</p> <p>BoundColumns also allows data to be formatted. As seen in the above declaration, the DataFormatString property is used to format the date and currency values. Format is extremely useful for representing localized texts and controlling non-string types.</p> <p>Step2apage.cs contains code that supports the details page.</p> <p>STEP2APAGE.CS:</p> <p>Namespace Samples {</p> <p>...</p> <p>Public Class Step2Apage: Page {</p> <p>Private Object CurrentAr;</p> <p>/ / Return to the currently selected Author</p> <p>Public Object CurrentAuthor {</p> <p>Get {</p> <p>Return CurrentAuthor;</p> <p>}</p> <p>}</p> <p>// Retrieve application data stored in session status</p> <p>Private dataset getsessionData () {</p> <p>Return (Dataset) session ["appdata"];</p> <p>}</p> <p>// Process the itemcreated event to customize the tail</p> <p>// Show summary information</p> <p>Protected Void OnItemcreatedTitlesgrid (Object Sender,</p> <p>DataGridItemcreatedEventArgs e) {</p> <p>IF (E.Item.ItemType == ListItemType.footer) {</p> <p>INT CELLCOUNT = E.Item.cells.count;</p> <p>For (int i = 0; i <cellcount - 1; i ) {</p> <p>E.Item.cells.Removeat (0);</p> <p>}</p> <p>Int itemcount = titlesgrid.Items.count;</p> <p>String itemcountstring;</p> <p>IF (itemcount == 0) {</p> <p>itemcountstring = "no titles found";</p> <p>}</p> <p>Else {</p> <p>itemcountstring = int32.toString (itemcount) </p> <p>"Title (s)";</p> <p>}</p> <p>TableCell Summarycell = E.Item.cells [0];</p> <p>Summarycell.text = itemcountstring;</p> <p>Summarycell.columnpan = cellcount;</p> <p>Summarycell.horizontalalign = horizontalalign.right;</p> <p>}</p> <p>}</p> <p>// Super-control ONLOAD to load the details of the selected author</p> <p>Protected Override Void OnLoad (Eventargs E) {</p> <p>Base.onLoad (e);</p> <p>String authorid = request.queryString ["authorid"];</p> <p>IF (Authorid! = NULL) {</p> <p>SelectAuthor (Authorid);</p> <p>Detailspanel.DATABIND ();</p> <p>}</p> <p>}</p> <p>/ / Set the CurrentAuth object according to the given author ID</p> <p>Private void selectautauthor (string authorid) {</p> <p>DataSet DS = GetSessionData ();</p> <p>DataView DV = DS.TABLES ["Author"]. Defaultview; dv.rowfilter = "au_id = '" authorid "" ";</p> <p>CurrentAuthor = DV [0];</p> <p>}</p> <p>}</p> <p>}</p> <p>By accessing the Request.QueryString collection, the class beyond the online ONLOAD method to retrieve the Author ID from the parameters in the URL request. The author ID is then used to query the data source and set the CurrentAr property. Finally, call DATABIND to make an expression for all data binding.</p> <p>In addition, the page implements an event processor for the ItemCreated event of the DataGrid control. The itemcreated event is one of the extension mechanisms provided by DataGrid for its advanced purposes. This event allows you to add and delete controls inside the row, and add it to a particular row in special circumstances.</p> <p>DataGrid triggered the event each time you create a project (or row). There are two cases:</p> <p>To be binding the control, and the project needs to be created from the head, the event is triggered before data binding the project. When you want to create a project from the save state during the round-trip process, the event is raised before loading the saved status into the project and the control therebetween.</p> <p>As a result, the event provides a hook for making changes to the existing control structure of the project.</p> <p>In this example, the processor modifies the end of the control to display a count of the column name. The number of columns included in the tail is the same as other rows. Abstract The entire DataGrid needs to be across the entire DataGrid. Therefore, the processor only retains a cell in the row, deletes all the rest, and resets the remaining cells of the COLUMNSPAN to make it cross the entire column, set its horizontalalign properties to make the text right alignment Finally, set its text, indicating the count.</p> <p>When processing the event, only one rule needs to be followed: You must perform the same structural conversion, regardless of the context of calling the item, whether in the data binding process, or during the round-trip process.</p> <p>Step 3: Master / Detail View in Single Pages</p> <p>Another optional and more common way to create a master / detail view is to display these views with a single page.</p> <p>Figure 4. Complete the page behind the third step</p> <p>Authors DataGrid From:</p> <p>Step3.aspx:</p> <p><ask: DataGrid ID = "authorsgrid" runat = "server"</p> <p>...</p> <p>DataKeyfield = "au_id"</p> <p>OnselectedIndexchanged = "OnselIndexchangeDAuthorsgrid"></p> <p><Property Name = "Column"></p> <p><asp: buttoncolumn text = "SELECT" Command = "SELECT" /></p> <p>...</p> <p></ Property></p> <p>...</p> <p><Property Name = "SelectedItemStyle"></p> <p><ask: tableitemstyle backcolor = "palegoldenrod" font-bold = "true" /></p> <p></ Property></p> <p></ ask: DataGrid></p> <p>There are three changes in this statement compared to the previous step.</p> <p>First, HyperLinkColumn added in step 2 has been deleted. This is no longer required because the entire view is implemented in a single page. Second, add a new column type ButtonColumn to the column set. This column generates a linkButton in each row for submitting this page instead of browsing from this page. The column's Command property is set to SELECT, and the corresponding LinkButton property is set. DataGrid Select the select as a standard command to select the column of the clicked button.</p> <p>When the selection changes, the DataGrid triggers the SELECTEDEXCHANGED event, which is processed in the code. The DatakeyField property page is set, resulting in a value of the DataKeys collection to fill the value corresponding to each item in the data source.</p> <p>This page also contains the UI and layout of the details of the details of the Detail section entitled to the panel of Detailspanel. This is replicated from the second stepwise.</p> <p>Step3page.cs represents the support code of the .aspx page in this step. These include code combinations from two code supported in step 2. Most of the code from the detailed information page is originally illuminated. The code shown below contains the changes and supplements we do.</p> <p>Step3page.cs:</p> <p>Namespace Samples {</p> <p>...</p> <p>Public Class Step3Page: Page {</p> <p>Private Object CurrentAr;</p> <p>// load the authors table in DataGrid</p> <p>/ / Another update author</p> <p>Private void loadingauthorsgrid () {</p> <p>Icollection authors = getAuthors ();</p> <p>Authorsgrid.datasource = authors;</p> <p>IF (authors.count! = 0) {</p> <p>Authorsgrid.selectedIndex = 0;</p> <p>}</p> <p>Else {</p> <p>Authorsgrid.selectedIndex = -1;</p> <p>}</p> <p>Authorsgrid.databind ();</p> <p>Updatedetails ();</p> <p>}</p> <p>// Treat the selected industryXchanged event in the author grid,</p> <p>// Update details</p> <p>Protected void OnselIndexchangeDauthorsgrid (Object Sender,</p> <p>Eventargs e) {</p> <p>Updatedetails ();</p> <p>}</p> <p>/ / Update according to the current authors</p> <p>// Details Festival</p> <p>Private void updateTails () {</p> <p>Updateselection ();</p> <p>IF (CurrentAuthor! = null) {</p> <p>Detailspanel.visible = true;</p> <p>Detailspanel.DATABIND ();</p> <p>}</p> <p>Else {</p> <p>Detailspanel.visible = false;</p> <p>}</p> <p>}</p> <p>// Update the author of the currently selected</p> <p>Private void updateselection () {</p> <p>Int selectedindex = authorsgrid.selectedIndIndex;</p> <p>CurrentAuthor = NULL;</p> <p>IF (SELECTEDINDEX! = -1) {</p> <p>String authorid =</p> <p>(String) Authorsgrid.DataKeys [SELECTEDINDEX];</p> <p>DataSet DS = GetSessionData ();</p> <p>DataView DV = DS.TABLES ["Author"]. DefaultView;</p> <p>Dv.rowfilter = "au_id = '" authorid "'"; currentauthor = DV [0];</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p>Each SELECTEDINDEX property is initialized each time you load Authors DataGrid. Once the binding is obtained, the DataKeys collection is completed, and the details of the details are updated by calling UpdatedTails.</p> <p>Details in the processor of the SelectedIndexchanged event is also updated. Note that the DATAKEYS collection is filled with a save state in the Save of DataBind until the call.</p> <p>UpdatedTails method first calls UpdateSerection. UpdateSerection uses Authors DataGrid's SELECTEDIDEX and DATAKEYS attribute to determine the ID of the selected author's ID and initialize the CurrentAuth property. Then, UpdatedTails call DataBind on the DetailsPanel control, and evaluate the data binding expressions accessed by the CurrentAutor.</p> <p>In addition to selecting the SelectIndexchanged event, you can also implement another key concept from this sample. Note that changing the selection does not need to have an Authors DataGrid over data binding. Therefore, it is never necessary to reload the AUTHORS list, which greatly reduces access to the Authors table. This is a key benefit of explicit data binding models implemented in ASP .</p> <p>Step 4: Sort</p> <p>DataGrid supports the generation of the clicking header, which can be used to let the end user sorted the data shown in the control. This step adds a function of sorting the AUTHORS list.</p> <p>Figure 5. Complete the screen snapshot of the page after step 4</p> <p>Authors DataGrid From:</p> <p>Step4.aspx</p> <p><ask: DataGrid ID = "authorsgrid" runat = "server"</p> <p>...</p> <p>ALLOWSORTING = "True"</p> <p>OnSortCommand = "OnSortCommandAuthorsgrid"</p> <p>OnItemcreated = "OnItemcreatedAuthorsgrid"></p> <p><Property Name = "Column"></p> <p><asp: buttoncolumn text = "SELECT" Command = "SELECT" /></p> <p><asp: boundcolumn headertext = "id" datafield = "au_id"></p> <p><Property Name = "Headerstyle"></p> <p><asp: tableitemstyle width = "100px" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p><asp: boundcolumn headertext = "name" datafield = "au_name"</p> <p>Sortfield = "au_name"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "150px" /></p> <p></ Property> </ asp: boundcolumn></p> <p><ask: boundcolumn headertext = "state" datafield = "state"</p> <p>Sortfield = "State"></p> <p><Property Name = "Headerstyle"></p> <p><asp: tableitemstyle width = "75px" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p></ Property></p> <p>...</p> <p></ ask: DataGrid></p> <p>Enable sort by setting the allowsorting to TRUE. Then, set the Sortfield property of each column that does not support sorting. Columns do not set this attribute do not generate clickable headers. Finally, the SortCommand event is handled, which will be discussed in the context of the file with code supported. The sample also adds an event processor to the ItemCreated event to display the current sort setting in the column header. The following code shows that the ordering function is added to the Authors list and make changes and supplements in a class with code support.</p> <p>Step4page.cs:</p> <p>Namespace Samples {</p> <p>...</p> <p>Public Class Step4page: Page {</p> <p>/ / Return to the current sort direction, this information is</p> <p>// maintained in the Page status</p> <p>Protected bool sortascending {</p> <p>Get {</p> <p>Object o = state ["sortascending"];</p> <p>IF (o! = null)</p> <p>Return (BOOL) O;</p> <p>Return True;</p> <p>}</p> <p>SET {</p> <p>State ["Sortascending"] = Value;</p> <p>}</p> <p>}</p> <p>/ / Return to the current sort field, this information is</p> <p>// maintained in the Page status</p> <p>protected string sortfield {</p> <p>Get {</p> <p>Object o = state ["sortfield"];</p> <p>IF (o! = null)</p> <p>Return (String) O;</p> <p>Return "AU_NAME";</p> <p>}</p> <p>SET {</p> <p>State ["Sortfield"] = Value;</p> <p>}</p> <p>}</p> <p>// Retrieve an Authors table</p> <p>PRIVATE ICOLLECTION GETAUTHORS () {</p> <p>DataSet DS = GetSessionData ();</p> <p>DataView DV = DS.TABLES ["Author"]. DefaultView;</p> <p>DV.ROWFILTY;</p> <p>String sort = sortfield;</p> <p>IF (sortascending == false) {</p> <p>Sort = "DESC";</p> <p>}</p> <p>Dv.sort = sort;</p> <p>Return DV;</p> <p>}</p> <p>// Process the itemcreated event to the header with a sort icon</p> <p>// customize</p> <p>Protected Void OnItemcreatedAuthorsgrid (Object Sender,</p> <p>DataGridItemcreatedEventArgs e) {</p> <p>IF (E.Item.ItemType == ListItemType.Header) {</p> <p>String sortfield = sortfield;</p> <p>Bool ascending = sortascending; label sortglyph = new label ();</p> <p>Sortglyph.Text = Ascending? "5": "6";</p> <p>Sortglyph.font.name = "Webdings";</p> <p>Tablecell Cell = NULL;</p> <p>IF (sortfield.equals) {</p> <p>Cell = E.Item.cells [2];</p> <p>}</p> <p>Else IF (Sortfield.equals ("State")) {</p> <p>Cell = E.Item.cells [3];</p> <p>}</p> <p>IF (Cell! = null) {</p> <p>Cell.controls.add (sortglyph);</p> <p>}</p> <p>}</p> <p>}</p> <p>// Treat the SortCommand event in the author grid to update</p> <p>// Sort parameter and reload the new sorted data</p> <p>Protected Void OnsortCommandauthorsgrid (Object Sender,</p> <p>DataGridsortCommandEventArgs e) {</p> <p>String currentsterfield = sortfield;</p> <p>Sortfield = E.sortfield;</p> <p>IF (currentsortfield.equals (e.sortfield) {</p> <p>Sortascending =! Sortascending;</p> <p>}</p> <p>Else {</p> <p>Sortascending = true;</p> <p>}</p> <p>LoadAuthorsgrid ();</p> <p>}</p> <p>}</p> <p>}</p> <p>The page uses the current field and sorting direction as the name value pair, retains the state attribute in the page, and provides it as a SortField and Sortascending properties, at which point the page is responsible for maintaining the values ​​of these attributes between the round trip processes.</p> <p>These properties are used for the GetAuthors function, where it is sorted according to the current value of the attribute before returning an Author list.</p> <p>Event processors for SortCommand events are passed in one instance of DataGridSortCommandeventArgs. This object contains the value of the sortfield property, and the property is associated with columns that are clicked in the title. You can choose to handle this value as long as it is reasonable in your application. For example, this value can include a name of a single field, such as the situation in this sample, or a sorting information, thereby allowing multiple column sorting.</p> <p>Implementing this method, updating the value of the sortfield and sortascending properties. Finally, this method calls loadAuthorsgrid and binds the Authors DataGrid. At this time, the situation is that the column of the DataGrid needs to be regenerated and requires you to set the data source and call DATABIND.</p> <p>The itemcreated event processor is similar to the event processor for Titles DataGrid in step 2. At this time, the processor modifies the control structure of the header row, and the specific method is to add icons representing the rise and fall, indicating that the currently sorted column and the current sorting direction.</p> <p>Here is a problem that needs to answer, why does DataGrid are not actually sorted by its data. This mainly has two main reasons. First, the ASP data source is a general ICollection so that you have the greatest flexibility when you select the data source. Data sources do not include built-in sequencing semantics. Second, it is also more important. During each request, automatic sorting will require an instance of a activity of the data source. This is not suitable for use in explicit data binding structures because this requires you to access and load data during the round-trip process. Step 5: Page 5</p> <p>DataGrid provides a function of displaying a special row that contains the UI for displaying the page number and "Next" / "Previous" browsing button. This step adds a feature to page viewing to the Authors list.</p> <p>Figure 6. Complete the screen snapshot of the page after step 5</p> <p>Authors DataGrid From:</p> <p>Step5.aspx:</p> <p><ask: DataGrid ID = "authorsgrid" runat = "server"</p> <p>...</p> <p>AllowPaging = "true" Pagesize = "5"</p> <p>ONPAGEINDEXCHANGED = "OnpageIndexchangeDAuthorsgrid"></p> <p>...</p> <p><Property Name = "PagersTyle"></p> <p><ask: DataGridPagerstyle BackColor = "Tan" horizontalalign = "Right"</p> <p>PageButtonCount = "3" Mode = "numericpages" /></p> <p></ Property></p> <p></ ask: DataGrid></p> <p>Similar to sorting, you must set several properties and implement an event processor to enable paging features. The specific step of enabling the paging feature is to set the AllowPaging property to True and set the PageSize property to a positive integer. The PageSize property determines the value of the data source to be expressed in a single page.</p> <p>The sample also shows setting the PagersTyle property, allowing you to customize the visual appearance and layout of the paging UI. The sample will show the pager at a time, the method is to set the MODE to NumericButtons, and set the pageButtonCount property to "3" and make it to the right. In addition to these settings, you can also choose "Next" / "Previous" Style button, the location of the Paging UI, its color scheme, and more.</p> <p>To support paging, you must handle the DataGrid's pageIndexchanged event, and see Step5Page.cs for specific discussion.</p> <p>Step5page.cs:</p> <p>Namespace Samples {</p> <p>...</p> <p>Public class step5page: Page {</p> <p>// Treat the PageIndexchanged event in the author grid to</p> <p>// update data</p> <p>Protected Void OnPageIndexchangeDauthorsgrid (Object Sender,</p> <p>DataGridPageChangeDeventArgs e) {</p> <p>LoadAuthorsgrid ();</p> <p>}</p> <p>}</p> <p>}</p> <p>PageIndexchangeDevent's event handler is just reloading the DataGrid, thereby automatically rendering paging views of all items in the data source. The event processor is passed in an instance of DataGridPageChangeDeventArg, which contains information about the old page index and the new page index. If you are determined by the application's status, you should not change the page index, you can set the old pages index with only the DataGrid's CURRENTPAGEIDEX property, skip the reloaded data. Explicit data binding model makes it possible. Here, the content of DataGrid is refreshed until the DATABIND is called. DataGrid supports an alternative paging mode, you can enable this mode by setting the AlLowCustomPaging property to TRUE. In this mode, you are responsible for telling the Total number of values ​​in the data source to the DataGrid, and the specific method is to set the VirtualItemCount property. Once in this mode, your data source will only contain the value of the current page. In this mode, you only need to retrieve the minimum number of values ​​from the query to further optimize your data access.</p> <p>Step 6: Editing in place</p> <p>DataGrid supports the original editing of its rows. This step uses this feature to allow editing inside the Titles DataGrid.</p> <p>Figure 7. Complete the page behind step 6</p> <p>Titles DataGrid From:</p> <p>Step6.aspx:</p> <p><ask: DataGrid ID = "titlesgrid" runat = "server"</p> <p>...</p> <p>OneDitCommand = "OneDitCommandtitlesgrid"</p> <p>OnupdateCommand = "OnupdateCommandtitlesgrid"</p> <p>Oncancelcommand = "oncancelcommandtitlesgrid"></p> <p><Property Name = "Column"></p> <p><ask: boundcolumn headertext = "id" datafield = "title_id" readonly = "true"></p> <p><Property Name = "Headerstyle"></p> <p><asp: tableitemstyle width = "100px" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p><asp: boundcolumn headertext = "title" datafield = "title" readonly = "true"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "250px" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p><asp: boundcolumn headertext = "public" datafield = "pubdate"</p> <p>DataFormatString = "{0: mmm yyyy}" readonly = "true"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "100px" /> </ property></p> <p></ ask: BOUNDCOLUMN></p> <p><asp: boundcolumn headertext = "price" datafield = "price"</p> <p>DataFormatString = "{0: C}"></p> <p><Property Name = "Headerstyle"></p> <p><ask: TableItemStyle Width = "50px" /></p> <p></ Property></p> <p><Property Name = "ItemStyle"></p> <p><ask: TableItemStyle Horizontalalign = "Right" /></p> <p></ Property></p> <p></ ask: BOUNDCOLUMN></p> <p><ask: editcommandcolumn edittext = "edit" canceltext = "ca Zancel"</p> <p>UpdateText = "OK" /></p> <p></ Property></p> <p>...</p> <p></ ask: DataGrid></p> <p>The purpose of editing in the DataGrid is reached by handling the editcommand, updateCommand, and CancelCommand events of the control.</p> <p>The Columns collection contains a new column named EditCommandColumn. This column automatically creates a button set on the right side of each row. Create an Edit button for the serials in the read-only mode and create an UPDATE and CANCEL buttons for each line in the edit mode. The EDITTEXT, CANCELTEXT, and UPDATEXT properties are used to specify the text of the button. Note that the text can be set to an HTML tag to use the image for these buttons.</p> <p>Finally, the READONLY <. / FONT> attribute of the columns is set to TRUE. This prevents editing of data in these columns, even if the column is in edit mode. For this example, only the price field is editable, so all other BoundColumns are labeled read-only.</p> <p>The above declared event processor is implemented in the class shown below. Step6page.cs: Namespace Samples {</p> <p>...</p> <p>Public class step6page: Page {</p> <p>/ / Update the currently selected author and reload the title corresponding to the selected content</p> <p>// grid</p> <p>protected void loadingtitlesgrid () {</p> <p>Updateselection ();</p> <p>Titlesgrid.databind ();</p> <p>}</p> <p>// Treat the Cancelcommand event in the book name grid,</p> <p>// End Edit without administration</p> <p>Protected Void Oncancelcommandtitlesgrid (Object Sender,</p> <p>DataGridCommandEventArgs e) {</p> <p>Titlesgrid.editIndEx = -1;</p> <p>LoadTitlesgrid ();</p> <p>}</p> <p>// Treply handle the editcommand event in the book name grid,</p> <p>// Start editing a row</p> <p>Protected Void OneDitCommandtitlesgrid (Object Sender,</p> <p>DataGridCommandEventArgs e) {</p> <p>Titlesgrid.edititeMindex = E.Item.ItemIndex;</p> <p>LoadTitlesgrid ();</p> <p>}</p> <p>// Trepate the UpdateCommand event in the book name grid to apply the // change and end the editor</p> <p>Protected void onupdateCommandtitlesgrid (Object Sender,</p> <p>DataGridCommandEventArgs e) {</p> <p>TextBox preformExt =</p> <p>(TextBox) E.Item.FindControl ("Column3Control");</p> <p>String newprice = prictext.text.substring (1);</p> <p>DataSet DS = GetSessionData ();</p> <p>DataTable TitleStable = ds.tables ["Title"];</p> <p>String TitleID =</p> <p>(String) Titlesgrid.DataKeys [E.Item.ItemIndex];</p> <p>DataRow [] rows = TitleStable.select ("Title_ID = '" </p> <p>TitleID "'");</p> <p>DataRow Editrow = ROWS [0];</p> <p>Editrow.beginedit ();</p> <p>Editrow ["price"] = newprice;</p> <p>Editrow.endedit ();</p> <p>Editrow.acceptchanges ();</p> <p>Titlesgrid.editIndEx = -1;</p> <p>LoadTitlesgrid ();</p> <p>}</p> <p>}</p> <p>}</p> <p>The EditCommand event is triggered when you click the Edit button. Simply set the DataGrid's EditIndex property to the properties of the index containing the clicked button. To prevent editing a certain one, return it without any action. Alternatively, you must continue editing, you must reload the data source and call DATABIND. This is done by calling the loadtitlesgrid method. The second event you must handle is the CancelCommand event. This is caused by clicking the CANCEL button. Implementation is very similar to the previous processor. To end the editing, just reset the EditItemIndex property to? And reload the data source. If you need to maintain the editing mode of the row, you only need to return any actions. The last event to be processed is the UpdateCommand event, which is raised when you click the Update button. Here is where practical work. Add a new value from the controls existing in the project, then update the data source. Once the new value is used, the EditItemIndex is reset to be to returned to read-only mode and reload the control along with its data source. For the first two events, you can return from this function without any action to maintain the editing mode status of the line. This step is again an exemplified data binding model implemented in the control. In this implementation, the data source is only required to change from read-only mode to edit mode or at a certain state. Note that DataGrid itself does not update its data source. In essence, data binding is one-way. The purpose of this model is to let you have control of the data source update. In most typical applications, the update requires pretreatment and is called by a business object or a stored process. In addition, one-way data binding does not require a real-time data source every time.</p> <p>Step 7: Use the template DataGrid control by using TemplateColumns to support the in-line template. The contents of the cells associated with these columns can be fully disciplined. This step uses TemplateColumn to enhance the editor supported in the previous step. Figure 8. Complete the page Titles DataGrid from step 7: Step7.aspx: <asp: datagrid id = "titlesgrid" runat = "server" ...></p> <p><Property Name = "Column"></p> <p>...</p> <p><ask: templatecolumn headertext = "price"></p> <p><template name = "itemtemplate"></p> <p><asp: label runat = "server"</p> <p>Text = '<% # DataBinder.eval (Container.DataItem, "Price",</p> <p>"{0: C}")%> '/></p> <p></ template></p> <p><template name = "edititemtemplate"></p> <p><ask: requiredfieldvaliDator runat = "server"</p> <p>ControlTovAlidate = "deflerxt" Display = "Dynamic"></p> <p><img src = "error.gif" Height = "16" width = "16" title = "required"</p> <p>Align = "absmiddle"></p> <p></ asp: requiredfieldValidator></p> <p><ask: regularExpressionValidator runat = "server"</p> <p>ControlTovAlidate = "deflerxt" Display = "Dynamic"</p> <p>ValidationExpression = "/ $ [0-9] (/. [0-9] [0-9]?)?">></p> <p><img src = "error.gif" Height = "16" width = "16"</p> <p>Title = "Invalid Currency Value"</p> <p>Align = "absmiddle"></p> <p></ asp: regularExpressionValidator></p> <p><ask: textbox id = "defler" runat = "server"</p> <p>Text = '<% # DataBinder.eval (Container.DataItem, "Price",</p> <p>"{0: C}")%> '</p> <p>Width = "45px" BorderStyle = "Solid" borderwidth = "1px"</p> <p>Bordercolor = "Black"></p> <p></ asp: textbox></p> <p></ template></p> <p><Property Name = "Headerstyle"> <asp: TableItemStyle Width = "50px" /></p> <p></ Property></p> <p><Property Name = "ItemStyle"></p> <p><ask: TableItemStyle Horizontalalign = "Right" /></p> <p></ Property></p> <p></ asp: templateColumn></p> <p></ Property></p> <p>...</p> <p></ ask: DataGrid></p> <p>Controls Declaration Shows TemplateColumn Add to Column Sets to replace the BoundColumn in the price field. TemplateColumns is another extension mechanism for DataGrid. It can be fully customized to the UI in the table format layout created by the DataGrid. TemplateColumn also provides template properties such as ItemTemplate and EditItemplate, so you can specify controls that should be used in cells associated with columns. In this example, ItemTemplate contains a Label control, which is bound to the price field. This is essentially imitating the function of BoundColumn. Interestingly, the EditItemTemplate template is used in the cell in edit mode. The sample puts a Textbox into the template that has been bound to the price field. This provides the same function as BoundColumn. The template contains a wide variety of enhancements. First, the template allows for greater control to TextBox, so you can create more attractive editing UIs. Second, allow you to place the verification control, so that you can verify it. In this sample, RequiredFieldValidator ensures that TextBox always contains a value, while RegulaRexpressionValidator ensures that the text contains a valid currency value. The most exciting aspect of this feature is to automatically verify client-end verification. The ASP Verification Control automatically verifies client-side authentication and enables error indicators on a feature-rich browser client, so that there is no need to go to and from the process and fall to the sub-client authentication. Step7page.cs: Namespace Samples {</p> <p>...</p> <p>Public Class Step7page: Page {</p> <p>// Treated the UpdateCommand event in the book name grid to apply</p> <p>// Changes made and end editing (when the page is active)</p> <p>Protected void onupdateCommandtitlesgrid (Object Sender,</p> <p>DataGridCommandEventArgs e) {</p> <p>IF (isvalid) {</p> <p>TextBox preformExt =</p> <p>(TextBox) E.Item.FindControl ("Prictext");</p> <p>String newprice = prictext.text.substring (1);</p> <p>DataSet DS = GetSessionData ();</p> <p>DataTable TitleStable = ds.tables ["Title"];</p> <p>String TitleID =</p> <p>(String) Titlesgrid.DataKeys [E.Item.ItemIndex];</p> <p>DataRow [] rows = TitleStable.select ("Title_ID = '" </p> <p>TitleID "'");</p> <p>DataRow editrow = rows [0]; editrow.beginedit ();</p> <p>Editrow ["price"] = newprice;</p> <p>Editrow.endedit ();</p> <p>Editrow.acceptchanges ();</p> <p>Titlesgrid.editIndEx = -1;</p> <p>LoadTitlesgrid ();</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p>The unique change of the support code is related to the validity of the page before updating the data source. Verify controls automatically update the IsValid property of the page. Check the effectiveness is done in the UpdateCommand event processor. As mentioned in step 6, the editing mode of the line will remain from the event processor without taking any action. Therefore, all update logic will be performed when and only when the page is in an active state. Confirm that the control will automatically display its error messages when it is invalid, and no supplemental code is required.</p> <p>Step 8: Customized columns As you can see, the DataGrid control supports standard columns, such as BoundColumn, ButtonColumn, and TemplateColumn. This control also allows you to extend control with your column type. These new columns provide a high degree of flexibility. This step implements a custom column named ImageColumn, which is used to display an image for each row with image URL data binding. Figure 9. Complete the page Titles DataGrid and ImageColumn from step 8 From: step8.aspx: step8.aspx: <% @ register tagprefix = "s" namespace = "samples"%></p> <p>...</p> <p><ask: DataGrid ID = "titlesgrid" runat = "server"</p> <p>...></p> <p><Property Name = "Column"></p> <p><s: imagecolumn imagefield = "title_id"</p> <p>Imageformatstring = "images / title- {0} .gif" /></p> <p></ Property></p> <p>...</p> <p></ ask: DataGrid></p> <p>This page contains a register instruction for mapping the <S: ImageColumn> tag to represent the Samples.ImageColumn class. DataGrid declaration display imageColumn adding to the columns collection of DataGrid. The ImageField and ImageFormatString sets are also shown, so the image is based on the URL of the title ID associated with a particular row. Its working principle is very similar to HyperLinkColumn used in step 2. ImageColumn.cs: Namespace Samples {</p> <p>...</p> <p>Public class imagecolumn: column {</p> <p>Private propertyDescriptor imagefielddsc;</p> <p>Public imagecolumn () {</p> <p>}</p> <p>Public string imagefield {</p> <p>Get {</p> <p>Object o = state ["imagefield"];</p> <p>RETURN (O! = null)? (String) o: string.empty;</p> <p>}</p> <p>SET {</p> <p>State ["Imagefield"] = Value;</p> <p>}</p> <p>}</p> <p>Public string imageformatstring {</p> <p>Get {</p> <p>Object o = state ["imageformatstring"]; return (o! = null)? (String) o: string.empty;</p> <p>}</p> <p>SET {</p> <p>State ["ImageformatString"] = Value;</p> <p>}</p> <p>}</p> <p>/ / Create in the cell associated with this column</p> <p>// control</p> <p>Public Override Void Initializecell (Tablecell Cell,</p> <p>Int columnIndex,</p> <p>ListitemType itemtype) {</p> <p>Base.initializecell (Cell, ColumnIndex, ItemType);</p> <p>IF (itemType == ListItemType.Item) ||</p> <p>(itemtype == ListiteMTYPE.AlternatingItem) ||</p> <p>(itemtype == ListiteMTYPE.SELECTEDITEM) ||</p> <p>(ItemType == ListItemType.editItem)) {</p> <p>Image image = new image ();</p> <p>Image.id = column.getColumnControlid (ColumnIndex, -1);</p> <p>Cell.Controls.Add (Image);</p> <p>IF (imagefield.length! = 0) {</p> <p>Image.addondataBind (New</p> <p>EventHandler (this.ondataBindColumn);</p> <p>}</p> <p>}</p> <p>}</p> <p>// Load the data into the control created in Initializecell</p> <p>Private Void OnDataBindColumn (Object Sender, Eventargs E) {</p> <p>Image BoundImage = (image) sender;</p> <p>DataGridItem Item = (DataGridItem) boundimage.namingcontainer;</p> <p>Object DataItem = item.dataItem;</p> <p>IF (isfirstdataBind ()) {</p> <p>String imagefield = imagefield;</p> <p>ImageFieldDesc =</p> <p>TypedScriptor.getProperties (DataItem) [ImageField];</p> <p>IF (ImageFieldDesc == NULL) {</p> <p>Throw New Exception ("Invalid Property: '" </p> <p>ImageField "'");</p> <p>}</p> <p>OnfirstDataBindHandLED ();</p> <p>}</p> <p>Object data = imagefieldDesc.getValue (DataItem);</p> <p>IF (Data! = NULL) {</p> <p>String Format = imageformatstring;</p> <p>IF (Format.length! = 0) {</p> <p>Boundimage.ImageURL = String.Format (Format, DATA);</p> <p>}</p> <p>Else {</p> <p>Boundimage.imageURL = data.toString ();</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p>Each column used by the DataGrid control is derived from an abstract column class. Column Types implement a variety of properties (such as headerText), and all the forms commonred in all column types. ImageColumn class is used to add properties for its specific features, such as ImageField and ImageFormatString properties. These properties are implemented by depositing each value in the list of states. The state of the column is automatically received during the round-trip process of the DataGrid control. The most important virtual method of each column is the initializecell method. DataGrid urges each column to initialize the cell associated with the column. When implementing the method, column creation the control it needs and add them as a sub-cell of the cell. The created control may vary from the ItemType parameter, which indicates the itemType property containing the column of the cell. The Column class itself contains logic for generating headers and tails. ImageColumn just creates an Image control when performing self-implementation. It also adds an event processor to Image's DataBind property. Once the column is created, DataGrid is binded to it. In this process, the columns are allowed to customize the controls created in Initializecell by means of data associated with columns. ImageColumn retrieves the value of the field it binds, generates a URL using the format specified by the user, then uses the result string to set the image of the image. Conclusion The DataGrid control simplifies a number of common web applications, including read-only reports to interactive application UI. This control is superior to traditional ASP programming. It converts the logical encapsulation of the converted object model and the data binding to the browser-independent HTML expression function. The retransmission data is also processed, and the details of the translation of the client event will be processed into the server event. The control is designed to express your data without having to make too many development efforts. As the application requirements change, and you start using DataGrid's various features, you can gradually add other features. This control is now available in Microsoft® .NET SDK and Framework. The SDK also includes supplemental documents and multiple samples that can be used to start; both implement the materials provided herein.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-101723.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="101723" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.050</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'u2DGiPHg_2F1gASUD92eqrJI_2Bw5GnyfcuxFZvAQvA18dN4b_2FofqXrvHHyBqHQtYtP8_2BKgdVrDXpXh0W3PlqvRTqw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>