Analysis of display controls in ASP. NET

Source: Internet
Author: User

Controls used to display data in Asp.net include repeater, datalist, and DataGrid.

The DataGrid is flexible and has a rich set of built-in events. It provides paging, editing, sorting, and other features, and is very convenient for table operations. It also has many optional styles, because it comes with many features and features, the development speed is fast and easy to deploy. However, its performance is not very high, and each operation will return to the server. It is mainly used for sorting, paging, editing, and multi-column display, and does not have high performance requirements. Generally, the data source is a dateset.

Datalist and DataGrid also have powerful template features and support data editing. The performance is better than that of DataGrid. The disadvantage is that the development cycle is relatively long than that of DataGrid, and there is no editor in the DataGrid, it is difficult to implement paging and sorting. It is mainly used in a single-column data list and a high-performance custom data table.

The repeater is completely rendered in HTML mode, which is more flexible and has the highest performance among the three display controls. However, editing, paging, and sorting are not supported. The development cycle is the longest. It is mainly used to display data with high performance and flexibility.

Notes when using the DataGrid

The DataGrid Control has a very heavy property autogeneratecolumns. The default value of this property is true. When it is true, all fields in the bound table are displayed, in actual application, we usually only need to display the desired field. In this case, we need to set the value of the autogeneratecolumns attribute to false, you can choose to display the columns on the control. If the column is set to automatically generate, do not specify the display column in The DataGrid. Otherwise, duplicate columns are generated.

You can find a control in a non-template column in The DataGrid using the datagriditem. cells [cells]. controls [Several controls] at this time, the subscript starts from 0, but when using the template column, the index of control must be added, because control [0] Is literalcontrol, if the value is null, it is equivalent to a space. In this case, it is best to obtain the datagriditem. findcontrol ["Control name.

When we add a button column to the DataGrid for deletion, we need to determine whether the data item to be deleted is the last one. If it is the last one, we should also determine whether the current page is the first one, if it is not the first page, the current index should be reduced by 1. Add the following code to the deletecommand event in the DataGrid.

If (this. DataGrid. Items. Count = 1)
{

If (this. DataGrid. currentpageindex! = 0)

{

This. DataGrid. currentpageindex = This. DataGrid. CurrentPageIndex-1;

}

}

When deleting a record, we should click the delete button to display a dialog box to determine whether to delete the record, add the following code to the itemdatabound event of the DataGrid control to implement this function and execute it on the client.

Switch (E. Item. itemtype)

{

Case listitemtype. item;

Case listietmtype. edititem;

Case listitemtype. alternatingitem;

Linkbutton lB = (linkbutton) E. Item. cells []. controls [0];

LB. attributs. Add ("onclick", "return confirm ('Are you sure you want to delete this record? ')");

Break;

}

The return in the last and second lines of the above code is very important and cannot be removed. Otherwise, the selected records will be deleted no matter whether you click "OK" or "cancel" in the pop-up dialog box.

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.