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

zhaozj2021-02-08  229

Nikhil Kothari

Microsoft Corporation

August 2000

Summary: The DataGrid control is a server control that is shipped with ASP PAGE Framework. This article uses DataGrid to create a web page with the main / detail view. Also discusses all aspects of the functions provided by the control, including sorting, editing, paging, and template columns, and the control is to use these features to establish the final page.

Download DataGridSamples.exe files. (119 kB)

table of Contents

Introduction What should we build? Step 1: A basic DataGrid Step 2: Main / Detail View with Multi-Page: Single Page Main / Details View Step 4: Sort Fepture Step 5: Page 6: Originally Edited Step 7: Using Templates Step 8: Custom Column Conclusions

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 (English) 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">

TYPE = "string"

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> <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"> <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></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>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>// 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) {</p> <p>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"> <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></p> <p><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"> <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></p> <p><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" 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><asp: tableitemstyle width = "100px" /></p> <p></ 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><ask: TableItemStyle BackColor = "Tan" /> </ 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.</p> <p>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>// 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;</p> <p>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> <property name =" columns "></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.</p> <p>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>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 "'";</p> <p>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"> <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></p> <p></ ask: 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; dv.rowfilter = string.empty;</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;</p> <p>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. 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 their 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.</p> <p>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 update the 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.</p> <p>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 = "published" datafield = "pubdate" dataformatstring = "{0: mmm yyyy}" 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 = "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>DataGridCommandev</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-1403.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="1403" 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.034</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 = 'eX4D5apgDGxyWnyTQSG_2FACVYYzxKoXMIF_2FqtksApqUH_2FJGdZ0_2FQ5ZyeQbl48yz3DZQvAh8J5jmXr65De5gV3Ng_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>