Tips for ASP. net2.0 gridview

Source: Internet
Author: User
1) When you bind a data source control to the gridview and need the edit or delete option button, the data source control must provide the SQ operation l statement or stored procedure call. In general, my Recommended Practice is, you can use meaningless SQL statements or stored procedures to generate the edit and delete buttons of the gridview. Code The runtime is not specified during the control design. Although a little more code is written, it will be very good for future extension applications. We recommend that you perform all data operations, whether simple or complex, all use a three-tier structure, so that the software conversion from web to win will be very easy. in addition, the three-layer structure is used in a unified manner. Although the amount of code is a little larger, the controllability is quite good. If the organization is reasonably unified, this approach is quite efficient.

2) You can specify the readonly attribute of the columns bound to the gridview as false, so that this column will not be automatically set to editable controls in the form of textbox when editing.

 

3) use the boundfield sub-control in the gridview control to bind a field column of a data table. You only need to set the datafield attribute of the boundfield control to the field name of the data table to be bound, if @ A is returned by the SELECT statement of the stored procedure, set datafield to A. You can use the dataformatstring attribute to format the field output. Note that you must set the htmlencode attribute to false; the headertext attribute is the field name displayed by the gridview. It is generally set to a Chinese field name. You need to set some other style attributes, such as the left alignment and background color, you can use the "style" attribute to select Settings. If the data bound to the field is null, the default value is displayed. You can set the nulldisplaytext field to display the settings.

4) To set the primary key of the gridview, you can set it in its datakeynames attribute. generally, if the data source returns five fields and you want to automatically generate columns, set autogeneratecolumns to true, if you want to select the columns you want to display or set the display effect, set autogeneratecolumns to false, and then in the edit column option operation of the gridview, set the datafield attribute of the boundfield control to the field name of the data table to be bound, and set the display accordingly.

 

5) The gridview has some common events, which I often use: rowcommand (as long as a button in the gridview control is pressed, it will be triggered, and you can find the current row of the gridview), rowdeleting (before deletion, do some judgment or preparation work, I suggest calling the data manipulation service method at the intermediate business layer directly here, if the deletion operation cannot be performed, you can set E. cancel = true, the specified SQL statement or stored procedure in the data source control will not be executed ),. rowdeleted (after deletion, do some Aftercare Work), rowupdating (Before update), rowupdated (after update), rowediting (triggered when you press the edit button), sorting (Before sorting ), sorted (after sorting), selectedindexchanging (when selecting a new row, no new action is triggered), selectedindexchanged (when selecting a new row and executing the operation ). it should be noted that all the events triggered before the operation "are suffixed with" ing ". Here we generally make some judgments to determine whether the operation conditions are met. If yes, then, the SQL statement or stored procedure of the data source control is executed during the design. However, if the conditions are not met, E. cancel = true. SQL statements and stored procedures are not executed.

6) in general, ASP. NET controls all have an attribute dedicated to processing client scripts, which is associated with someJavascriptThe client script code will be executed with the highest priority for selection or judgment, such as javascript: event. returnvalue = Window. confirm ("Do you really want to delete this sales order? "), Returns true, and processes the events bound to this control.ProgramIf false is returned, it is as if you did not click this control, and the server code is not executed downward.

7) Some button-type controls (buttonfield) inside the gridview, the processing events triggered when the click is the same, can be distinguished by setting different strings of the commandname attribute, such, in the rowcommand event, you can use. the commandname attribute is used to obtain whether the commandname of a specified buttonfield is the same as that of a specified character string.

8) the datasource attribute of the gridview is bound to the collection type, such as arrays, generic classes, dataset, and able. the performanceid attribute is bound to a specific data source control, such as sqldatasource, objectdatasource, and xmldatasource. then, call the databind () method to bind data. Before databind (), you only specify the data source.

9) Tips: When a gridview is bound to an object set, if the public attribute of the object is English, the column name of the gridview is also English. If the public attribute of the object is Chinese, the column name of the gridview is also Chinese. This technique can be used to set the properties of the gridview and obtain the gridview report for displaying the Chinese fields. it is a good solution for some special situations.

2. Add all options for the gridview
<Asp: templatefield headertext = "select all">
<Headertemplate>
<Asp: checkbox id = "checkall" runat = "server" text = "select all"
Autopostback = "true" Oncheckedchanged = "checkall "/>
</Headertemplate>
<Itemtemplate>
<Asp: checkbox id = "check" runat = "server" text = "select"/>
</Itemtemplate>
</ASP: templatefield>

protected void checkall (Object sender, eventargs e)
{< br> checkbox cb = (checkbox) sender;
If (CB. TEXT = "select all")
{< br> foreach (gridviewrow gvr in gvlessons. rows)
{< br> checkbox CB1 = (checkbox) gvr. findcontrol ("check");
cb1.checked = CB. checked;
}< br> }< BR >}

Related Article

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.