Gridview events and usage

Source: Internet
Author: User
Event Name Description
Databinding occurs when the server control is bound to the data source. (Inherited from control .)
Databound occurs after the server control is bound to the data source. (Inherited from basedataboundcontrol .)
Disposed occurs when the server control is released from the memory. This is the final stage of the server control survival when ASP. NET pages are requested. (Inherited from control .)
Init occurs when the server control is initialized. Initialization is the first step of the control's survival. (Inherited from control .)
Load occurs when the server control is loaded to the page object. (Inherited from control .)
Pageindexchanged occurs when a page navigation button is clicked, but after the pagination operation is processed by the gridview control.
Pageindexchanging occurs before the gridview control handles paging operations when you click the navigation button on a page.
The prerender occurs after the control object is loaded and before it is rendered. (Inherited from control .)
Rowcancelingedit: After clicking the cancel button for a row in edit mode, the row occurs before exiting edit mode.
Rowcommand occurs when you click the button in the gridview control.
Rowcreated occurs when you create a row in the gridview control.
Rowdatabound occurs when data rows are bound to data in the gridview control.
Rowdeleted occurs after the gridview control deletes a row when you click the delete button of a row.
Rowdeleting occurs before the gridview control deletes a row when you click the delete button of a row.
Rowediting occurs after you click the "edit" button of a row, and the gridview control enters the editing mode.
Rowupdated occurs after you click the "Update" button of a row and the gridview control updates the row.
Rowupdating occurs when the "Update" button of a row is clicked, And the gridview control updates the row.
Selectedindexchanged occurs when the "select" button of a row is clicked, And the gridview control processes the selection operation.
Selectedindexchanging occurs after you click the "select" button for a row, and the gridview control processes the selection operation.
When you click a hyperlink for column sorting, sorted occurs after the gridview control processes the corresponding sorting operation.
When you click a hyperlink for column sorting, sorting occurs before the gridview control processes the corresponding sorting operation.
Unload occurs when the server control is detached from the memory. (Inherited from control .)
Coming soon ~~

1. Order of events initialized by gridview binding
The gridview displays the bound data (5 rows by default ):
Databinding
Rowcreated: Header [0]
Rowdatabound
Rowcreated: datarow [1]
Rowdatabound
Rowcreated: datarow [2]
Rowdatabound
Rowcreated: datarow [3]
Rowdatabound
Rowcreated: datarow [4]
Rowdatabound
Rowcreated: datarow [5]
Rowdatabound
Rowcreated: footer [6] // This event will occur no matter whether there are any corner lines.
Rowdatabound
Rowcreated: pager [7]
Rowdatabound
Databound

The order is as follows:
Databinding
Rowcreated
Rowdatabound
......
Databound
2. Order of events when the page button is clicked in the gridview:
Rowcommand
Pageindexchanging
Pageindexchanged
Databinding
Rowcreated: Header [8]
Rowdatabound
Rowcreated: datarow [9]
Rowdatabound
Rowcreated: datarow [10]
Rowdatabound
Rowcreated: datarow [11]
Rowdatabound
Rowcreated: datarow [12]
Rowdatabound
Rowcreated: datarow [13]
Rowdatabound
Rowcreated: footer [14]
Rowdatabound
Rowcreated: pager [15]
Rowdatabound
Databound

Summary:
After the data is bound to the gridview, that is, after the databounding event is triggered, the data has been retrieved from the data source or the specified data fields will be retrieved, the value of the field with the visible attribute false is not obtained from the data source. This is why the value of a column is not obtained when the gridview hides a column using visible = false directly in HTML. Then, the rowcreated event is called to fill a row of data into a gridviewrow, and then the rowdatabound event is triggered to bind the row to the rows of the gridview. Until the last trigger of the gridview databound to bind the display data.

 

The difference between rowcreated and rowdatabound in the gridview is that the rowcreated event is like this. No matter whether the data is rebound to the gridview or not, adding a row of record in the gridview will trigger the rowcreated event. This means that when a PostBack callback event occurs, the rowcreated event is triggered even if the data comes from the view status.

The reason we use the rowcreated event instead of the rowdatabound event is that the rowdatabound event is triggered only when the data is explicitly bound to the Data web control.

 

Usage of gridview

I. general column attributes
1. boundfield
This type of column displays the fields in the data table in text.

2. checkboxfield (not commonly used)
Commonly used to display values of Boolean fields in a database

3. hyperlinkfield
Display a link. This type of field is commonly used and its main attribute is:
Datanavigateurlfields, datanavigateurlformatstrING, datatextfield, datatextformatstring
For example, to form a link in this column, the link text is "company name", link to the page for viewing Company details: viewcompany. aspx, and the page must be followed by a parameter id = XXXX. The first record ID = alfki, companyName = Alfreds futterkiste. The generated link is viewcompany. aspx? Id = alfki. The link text is "Alfreds futterkiste". The values of the above four attributes are defined as follows:
The value of datanavigateurlfields is "mermerid"
DatanavigateurlformatstrThe value of ing is "viewcompany. aspx? Id = {0} ", {0} will be replaced by the value of the field defined in the datanavigateurlfields attribute during production.
The value of datatextfield is "companyName"
The value of datatextformatstring is "{0}", that is, only the company name is displayed. No additional text is required. Shows the display effect:

4. imagefield (not commonly used)
This field is used to display the data of binary fields storing images. The image data stored in binary fields is directly displayed as images. However, after vs2005, this field removes the datafield attribute and adds the dataimageurlfield field, this means that you no longer need to directly display the binary content as an image. Instead, you need to write another display page to read and output the binary field content as the image content. This field is not commonly used.

5. buttonfield
This column is displayed as "button". Note: The button is not just <input type = "button"...> in Asp.net, controls that can submit forms (pages) to server actions can be called "buttons". Therefore, this type of field has an attribute: buttontype has three options: Link, image, and button, which indicate three forms of "button, corresponding to the linkbutton, imagebutton, And button controls in the toolbox.
When the button is clicked, the "rowcommand" event of the gridview will be triggered. After the button is clicked, the program to be executed should be written in the rowcommand event.
There is a problem: If a gridview contains multiple button columns but only one rowcommand event, how can we know which column of the button is clicked now? This must be specified through the commandname attribute of the buttonfield column.
For example, if there are two buttonfield columns, set the commandname attribute of the first column to "This is the first column" and set the commandname attribute of the second column to "this is the second column ".
Select the gridview control and double-click the space after the rowcommand event on the "events" page of the "Property panel.

In the rowcommand event of the gridview in the Aspx. CS file, the program is written as follows:
  Protected void gridview1_rowcommand (Object sender, gridviewcommandeventargsE)
  {
      Switch (E. commandname)
      {
          Case "This is the first column ":
              // Write the transaction to be processed after being clicked.
              Response. Write ("the first column is clicked ");
              Break;
          Case "this is the second column ":
              // Write the transaction to be processed after being clicked.
              Response. Write ("the second column is clicked ");
              Break;
      }
  }

6. commandfield
The essence of this column is no different from that of the buttonfield column. The only difference is that you do not need to set the commandname attribute. Instead, each button corresponds to a type of event in the gridview, rather than a rowcommand event, such: the Edit button corresponds to the rowediting event, and the delete button corresponds to the rowdeleting (at the time of deletion) and rowdeleted (after deletion) events, and so on. Here we will not list them one by one. And I will not give an example.

Ii. template Columns
The preceding columns are collectively referred to as "regular columns". The difference between the template column and the previous columns is that the previous columns are equivalent to a single control, this column is equivalent to a container. You can drag multiple controls into the container to create more complex cell content. The reason why the gridview is flexible is that it is flexible in this template column. In the column definition dialog box, add a template column, set only headtext and itemstyle. Width attributes, and click OK to close the dialog box. On the ASPX page, select the gridview control and click the right arrow, as shown in:

Click "Edit template" to go to the following page:

You can see that the name of the template column is listed in the drop-down list on the right. If there are multiple template columns, the names of Multiple Template columns are listed. Each template has multiple templates, generally, we only need to edit the itemtemplate. Other templates can be inferred based on their names.
Itemtemplate is the "Container". You can drag the control in the "toolbox" into it. Drag an image control, a button control, and a checkbox control.
Select these controls and change their IDs to meaningful names on the property panel. The specific usage will be discussed later.
After the template column is edited, click the right arrow and select "End template editing ".

3. rowdatabound event
The template column is defined above. If you want it to display the corresponding information of each record, you also need to assign values to the control attribute in the template column, this action is generally completed in the rowdatabound event of the gridview. Double-click the action page of the property panel to define a rowdatabound event. Assuming that the IDs of the three controls just defined are imgtemp, btntemp, and cbtemp, The Aspx. CS file demonstration program is as follows:
  Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
  {
      If (E. Row. rowindex <0) // If the header is being generated
          Return;
      Datarowview DRV = (datarowview) E. Row. dataitem; // Retrieve the data row view bound to the current row

      Image IMG = (image) E. Row. findcontrol ("imgtemp ");
      IMG. imageurl = DRV ["imageurl"]. tostring (); // Assume that the data table contains an imageurl field.

      Button BTN = (button) E. Row. findcontrol ("btntemp ");
      BTN. Text = DRV ["companyName"]. tostring ();

      Checkbox cb = (checkbox) E. Row. findcontrol ("cbtemp ");
      CB. Checked = (DRV ["isselect"]. tostring () = "1 "? True: false ); // Assume that there is an isselect field in the data table. 1 indicates selected, and 0 indicates not selected.
  }

The above example is not completely set according to the fields in the customer table of the demo table. The process is as follows:
1. Forcibly convert E. Row. dataitem to a datarowview object. This object is bound to a datarow view in the data table able of this row. Its usage is similar to that of datarow.
2. Use the E. Row. findcontrol method to find the control in the template with the specified ID.
3. Convert the value in datarowview to the property of the control through a certain calculation.

Not only can the template column be operated in the rowdatabound event, but also the regular column, but not the findcontrol method. The following example shows how, to obtain the hyperlink object in the regular hyperlinkfield column named 2nd columns, write the following program in the rowdatabound event:

Hyperlink HL = (Hyperlink) E. Row. cells [1]. controls [0];

Because this column is located in the second column of the table, cells [1] indicates the cell. This cell has only one control, so it is controls [0].
Another example is to add a javascript action to the delete button in the delete action column. Each time you click the delete button, a confirmation dialog box is displayed on the page through javascript. You can follow these steps:
1. Add a "delete" button column in the column editing dialog box. The button type is "button", which is assumed to be the 4th column in the table.
2. Write in rowdatabound as follows:

Button btdel = (button) E. Row. cells [3]. controls [0];
Bt. Attributes. Add ("onclick", "Return confirm ('Are you sure you want to delete this record? ')");

When you view the HTML source code of this table in a browser, you will find that each delete button has such an HTML attribute:
Onclick = "Return confirm ('Are you sure you want to delete this record? ')"
Return confirm ('..... ') is a javascript statement. The returned value is determined based on the items selected in the pop-up dialog box. If yes is selected, true is returned, the submission form triggered by this button continues. Otherwise, if false is returned, the submission form event triggered by this button stops, and the deletion action is not completed.

4. datakeys attributes
Gridview has a datakeys attribute that is frequently used. Here we will talk about it separately. For example, there is a delete button column. After you click Delete, you want to delete the current row. But how do you get the value of the key field of the current row from the background program? There are multiple methods, but the most reliable method is through the datakeys attribute of the gridview. The following procedures are examples:
1. When binding data, set the value of the datakeynames attribute:
      This. gridview1.datasource = Ds. Tables [0];
      This. gridview1.datakeynames = new string [] {"mermerid", "companyName "};
      This. gridview1.databind ();
The role of this program is to put the values of the customerid and companyName fields into the datakeys array.

2. Write the following program in the gridviewdomainrowdeleting event:
      String id = This. gridview1.datakeys [E. rowindex] ["mermerid"]. tostring ();
      String name = This. gridview1.datakeys [E. rowindex] ["companyName"]. tostring ();
In this way, the value of the current row record in the datakeys array is obtained.

5. Events

Databinding It occurs when the server control is bound to the data source. (Inherited from control .)
Databound This occurs when the server control is bound to the data source. (Inherited from basedataboundcontrol .)
Disposed When a server control is released from the memory, this is the final stage of the server control survival when ASP. NET pages are requested. (Inherited from control .)
Init It occurs when the server control is initialized. Initialization is the first step of the control's lifetime. (Inherited from control .)
Load This occurs when the server control is loaded to the page object. (Inherited from control .)
Pageindexchanged occurs when a page navigation button is clicked, but after the pagination operation is processed by the gridview control.
Pageindexchanging occurs before the gridview control handles paging operations when you click the navigation button on a page.
Prerender Occurs after the control object is loaded and before it is rendered. (Inherited from control .)
Rowcancelingedit: After clicking the cancel button for a row in edit mode, the row occurs before exiting edit mode.
Rowcommand occurs when you click the button in the gridview control.
Rowcreated occurs when you create a row in the gridview control.
Rowdatabound occurs when data rows are bound to data in the gridview control.
Rowdeleted occurs after the gridview control deletes a row when you click the delete button of a row.
Rowdeleting occurs before the gridview control deletes a row when you click the delete button of a row.
Rowediting occurs after you click the "edit" button of a row, and the gridview control enters the editing mode.
Rowupdated occurs after you click the "Update" button of a row and the gridview control updates the row.
Rowupdating occurs when the "Update" button of a row is clicked, And the gridview control updates the row.
Selectedindexchanged occurs when the "select" button of a row is clicked, And the gridview control processes the selection operation.
Selectedindexchanging occurs after you click the "select" button for a row, and the gridview control processes the selection operation.
When you click a hyperlink for column sorting, sorted occurs after the gridview control processes the corresponding sorting operation.
When you click a hyperlink for column sorting, sorting occurs before the gridview control processes the corresponding sorting operation.
Unload This occurs when the server control is detached from the memory. (Inherited from control .)

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.