DocumentData Binding control comparison (Reapter \ DataList \ GridView \ DatailsView \ FormView ):
1. Insert functions:
DetailsView and FormView have the insert function. Other controls do not have
2. Template
DataList \ FormView \ Repeater three templates must be edited, while
The GridView and DetailsView templates are only available after the columns are converted into template columns.
3. Automatic Paging
GridView, DetailsView, and FormView are both new controls in version 2.0, with built-in paging and sorting functions,
Others need to be manually defined
4. Data presentation method:
Reference content is as follows: GridView, DataList, and Repeator are used to present multiple columns of data, DetailsView and FormView are used to present single-column data, that is, common data details. DataList and Reapter both need to edit the template column, while TextBox can be added to the template column, and the TextBox ID can be specified to extract user input values, however, the DataGrid and the GridView do not need to edit the template. Its editing function is automatically generated. We cannot know the ID of those text boxes, so we cannot get the user input through the ID, you can reference cells to achieve the following: Private void DataGrid1_UpdateCommand (object source, xx) { String bkid = maid [e. Item. ItemIndex]. toString (); // extract the primary key String bktitle = (TextBox) e. Item. Cells [1]. Controls [0]). Text; // extract user input } |
1. Enter the editing status:
Reference content is as follows: DataList1.EditItemIndex = e. Item. ItemIndex; DataGrid1.EditItemIndex = e. Item. ItemIndex; GridView1.EditIndex = e. NewEditIndex; DetailsView1.ChangeMode (DetailsViewMode. Edit); // enter the editing status DetailsView1.ChangeMode (DetailsViewMode. ReadOnly); // exit the editing status. |
2. Set the primary key:
Reference content is as follows: DataList1.DataKeyField = "bkid "; DataGrid1.DataKeyField = "bkid ";String [] str = {"bkid "}; GridView1.DataKeyNames = str; |
Iii. Primary Key extraction:
Reference content is as follows: String bkid = DataList1.DataKeys [e. Item. ItemIndex]. ToString (); // DataList String bkid = maid [e. Item. ItemIndex]. ToString (); // DataGrid String bkid = GridView1.DataKeys [e. RowIndex]. Value. ToString (); // GridView String bkid = DetailsView1.DataKey [0]. ToString (); |
4. Search controls:
Reference content is as follows: String bktitle = (TextBox) e. Item. FindControl ("txtTile"). Text; // DataList String bktitle = (TextBox) e. Item. Cells [1]. Controls [0]). Text; // DataGrid String bktitle = (TextBox) GridView1.Rows [e. RowIndex]. Cells [1]. Controls [0]). Text; String bktitle = (TextBox) DetailsView1.Rows [1]. Cells [1]. Controls [0]). Text; |
Note that there are two ways to search controls: (you can use the following two methods to search for each data-bound control)
1. If you know the Control ID, you can use this method.
(TextBox) e. Item. FindControl ("txtTile"). Text; // This is search
2. This method is available if you do not know the Control ID
(TextBox) e. Item. Cells [1]. Controls [0]). Text; // This is an index
5. Add confirmation to the delete button:
Reference content is as follows: Protected void DataList1_ItemDataBound (object sender, DataListItemEventArgs e) { If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem) { LinkButton lbtn = (LinkButton) e. Item. FindControl ("lbtndelete "); Lbtn. Attributes. Add ("OnClick", "return confirm ('Are you sure you want to delete it? ')"); } }Protected void maid (object sender, maid e) { If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem) { LinkButton lbtn = (LinkButton) e. Item. Cells [3]. Controls [0]; Lbtn. Attributes. Add ("OnClick", "return confirm ('Are you sure you want to delete? ')"); } } Protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e) { If (e. Row. RowType = DataControlRowType. DataRow) { String strid = e. Row. Cells [0]. Text; // obtain the field value of the first Row; E. Row. Cells [3]. Attributes. Add ("OnClick", "return confirm ('Confirm to delete \" "+ strid + "\"? ')"); // Two escape characters are used to enclose the values in the first column in quotation marks. Note that the next escape character will not be interpreted and will be placed directly; } |