Use the ASP + DATAGRID control to create a main viewDetail view (2)

zhaozj2021-02-08  232

Step6page.cs:

Namespace Samples {

...

Public class step6page: Page {

/ / Update the currently selected author and reload the title corresponding to the selected content

// grid

protected void loadingtitlesgrid () {

Updateselection ();

Titlesgrid.databind ();

}

// Treat the Cancelcommand event in the book name grid,

// End Edit without administration

Protected Void Oncancelcommandtitlesgrid (Object Sender,

DataGridCommandEventArgs e) {

Titlesgrid.editIndEx = -1;

LoadTitlesgrid ();

}

// Treply handle the editcommand event in the book name grid,

// Start editing a row

Protected Void OneDitCommandtitlesgrid (Object Sender,

DataGridCommandEventArgs e) {

Titlesgrid.edititeMindex = E.Item.ItemIndex;

LoadTitlesgrid ();

}

// Treated the UpdateCommand event in the book name grid to apply

// Changes made and end editing

Protected void onupdateCommandtitlesgrid (Object Sender,

DataGridCommandEventArgs e) {

TextBox preformExt =

(TextBox) E.Item.FindControl ("Column3Control");

String newprice = prictext.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.editIndEx = -1;

LoadTitlesgrid ();

}

}

}

The EditCommand event is triggered when you click the Edit button. Simply set the DataGrid's EditIndex property to the properties of the index containing the clicked button. To prevent editing a certain one, return it without any action. Alternatively, you must continue editing, you must reload the data source and call DATABIND. This is done by calling the loadtitlesgrid method.

The second event you must handle is the CancelCommand event. This is caused by clicking the CANCEL button. Implementation is very similar to the previous processor. To end the editing, just reset the EditItemIndex property to? And reload the data source. If you need to maintain the editing mode of the row, you only need to return any actions.

The last event to be processed is the UpdateCommand event, which is raised when you click the Update button. Here is where practical work. Add a new value from the controls existing in the project, then update the data source. Once the new value is used, the EditItemIndex is reset to be to returned to read-only mode and reload the control along with its data source. For the first two events, you can return from this function without any action to maintain the editing mode status of the line. This step is again an exemplified data binding model implemented in the control. In this implementation, the data source is only required to change from read-only mode to edit mode or at a certain state. Note that DataGrid itself does not update its data source. In essence, data binding is one-way. The purpose of this model is to let you have control of the data source update. In most typical applications, the update requires pretreatment and is called by a business object or a stored process. In addition, one-way data binding does not require a real-time data source every time.

Step 7: Using Templates

The DataGrid control supports the in-line template by using TemplateColumns. The contents of the cells associated with these columns can be fully disciplined. This step uses TemplateColumn to enhance the editor supported in the previous step.

Figure 8. Complete the page behind step 7

Titles DataGrid From:

Step7.aspx:

...>

...