asp+|datagrid| Create | control | View Step6Page.cs:
Namespace Samples {
...
public class Step6page:page {
Updates the currently selected author and reloads the title that corresponds to the selection
Grid
protected void Loadtitlesgrid () {
Updateselection ();
Titlesgrid.databind ();
}
Handles the CancelCommand event in the title grid to
End edits without applying changes
protected void Oncancelcommandtitlesgrid (object sender,
DataGridCommandEventArgs e) {
Titlesgrid.edititemindex =-1;
Loadtitlesgrid ();
}
Handles the EditCommand event in the title grid to
Start editing a row
protected void Oneditcommandtitlesgrid (object sender,
DataGridCommandEventArgs e) {
Titlesgrid.edititemindex = E.item.itemindex;
Loadtitlesgrid ();
}
Handles the UpdateCommand event in the title grid to apply
Changes made and ends the edit
protected void Onupdatecommandtitlesgrid (object sender,
DataGridCommandEventArgs e) {
TextBox Pricetext =
(TextBox) E.item.findcontrol ("Column3control");
String newprice = priceText.Text.Substring (1);
DataSet ds = Getsessiondata ();
DataTable titlestable = ds. tables["Title"];
String TitleID =
(string) Titlesgrid.datakeys[e.item.itemindex];
datarow[] rows = Titlestable.select ("title_id = '" +
TitleID + "'");
DataRow editrow = rows[0];
Editrow.beginedit ();
editrow["price"] = Newprice;
Editrow.endedit ();
Editrow.acceptchanges ();
Titlesgrid.edititemindex =-1;
Loadtitlesgrid ();
}
}
}
The EditCommand event is raised when the Edit button is clicked. Simply set the EditItemIndex property of the DataGrid to the property of the index of the item containing the clicked button. To prevent an individual row from being edited, return without taking any action. In addition, to continue editing, you must reload the data source and invoke DataBind. This is done by invoking the Loadtitlesgrid method.
The second event that you have to deal with is the CancelCommand event. This is raised when the Cancel button is clicked. Implementation is very similar to the previous processor. To end editing, simply reset the EditItemIndex property to?, and reload the data source. If you need to maintain the edit mode of the row, just return it from the function without taking any action.
The last event to be processed is the UpdateCommand event, which is raised when the Update button is clicked. This is where the actual work takes place. Extract a new value from a control that exists in the project, and then update the data source. Once you have finished using the new value, reset the EditItemIndex to? To return to read-only mode and reload the control with its data source. For the first two events, you can return from the function without taking any action to maintain the edit mode state of the row.
This step again illustrates the explicit data-binding model implemented in the control. In this implementation scenario, the data source is required only when the state of a row is changed from read-only to edit mode or vice versa. Note that the DataGrid itself does not update its data source. In essence, data binding is one-way. The purpose of this model is to give you control over the update of the data source. In most typical applications, updates require preprocessing and are invoked by business objects or stored procedures. In addition, one-way data binding does not require a real-time data source for every time.
--------------------------------------------------------------------------------
7th step: Working with templates
The DataGrid control supports column-inside templates by using TemplateColumns. The contents of the cells associated with these columns can be completely freely determined. This step uses TemplateColumn to enhance the editing support that was first implemented in the previous step.
Figure 8. Complete the page after step 7th
The Titles DataGrid comes from:
Step7.aspx:
<asp:datagrid id= "Titlesgrid" runat= "Server"
...>
<property name= "Columns" >
...
<asp:templatecolumn headertext= "Price" >
<template name= "ItemTemplate" >
<asp:label runat= "Server"
Text= ' <%# DataBinder.Eval (Container.DataItem, "price",
"{0:c}")%> '/>
</template>
<template name= "EditItemTemplate" >
<asp:requiredfieldvalidator runat= "Server"
Controltovalidate= "Pricetext" display= "Dynamic" >
align= "Absmiddle" >
</asp:RequiredFieldValidator>
<asp:regularexpressionvalidator runat= "Server"
Controltovalidate= "Pricetext" display= "Dynamic"
Validationexpression= "\$[0-9]+ (\.[ 0-9][0-9]? " >
title= "Invalid Currency value"
align= "Absmiddle" >
</asp:RegularExpressionValidator>
<asp:textbox id= "Pricetext" runat= "Server"
text= ' <%# DATABINDER.E