http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatalistclassedititemtemplatetopic.asp<%@ Page Language = "C #" AutoEventWireup = "True"%> <% @ Import namespace = "system.data"%>
. // The Cart and CartView objects temporarily store the data source // for the DataList control while the page is being processed DataTable Cart = new DataTable (); DataView CartView; void Page_Load (Object sender, EventArgs e) {// With a database, use an select query to retrieve the data // Because the data source in this example is an in-memory // DataTable, retrieve the data from session state if it exists;.. // otherwise, create the data source GetSource ( );
// The DataList control maintains state between posts to the server; // it only needs to be bound to a data source the first time the // page is loaded or when the data source is updated if (IsPostBack!) {String [. ] slist = new string [] {"a", "b", "c"}; DataGrid1.datasource = slist; DataGrid1.dataBind ();}}
Void Bindlist (Datalist Itemslist) {
// set the data source and bind to the datalist control. Itemslist.datasource = cartview; itemslist.database ();
}
Void getSource () {
// for this Example, The Data Source IS A DataTable That // is Stored In Session State. If The Data Source Does Not Exist, // Create It; OtherWise, Load The Data. IF (Session ["ShoppingCart"] == NULL) {// Create The Sample Data. Datarow DR; // Define The Column of The Table. Cart.columns.Add (New Datacolumn ("Qty", Typeof (INT32)); Cart.columns.Add (New Datacolumn ("Item", TypeOf (String)); Cart.columns.Add (New Datacolumn ("Price", TypeOf (Double));
// store the table in session state to persist its value the server. Session ["shippingcart"] = cart; // populate the datable with sample data. For (int i = 1; i <= 5; i ) {DR = cart.newrow (); if (i% 2! = 0) {DR [0] = 2;} else {DR [0] = 1;} DR [1] = "Item" i. Tostring (); DR [2] = (1.23 * (i 1)); Cart.Rows.Add (DR);}
}
Else {
// Retrieve The Sample Data from session state. Cart = (DATATABLE) Session ["ShoppingCart"];
} // create a dataview and specify the field to sort by. CartView = new data; cartView.sort = "item";
Return;
}
Void Edit_Command (Object Sender, DataListCommandeventArgs E) {
// Set the EditItemIndex property to the index of the item clicked // in the DataList control to enable editing for that item. Be sure // to rebind the DataList to the data source to refresh the control. DataList ItemsList = (DataList) sender ItemsList.editItemIndex = E.Item.itemindex; bindlist (itemslist);
Void Cancel_Command (Object Sender, DataListCommandeventArgs E) {
// Set the EditItemIndex property to -1 to exit editing mode Be sure // to rebind the DataList to the data source to refresh the control DataList ItemsList = (DataList) sender;.. ItemsList.EditItemIndex = -1; BindList (ItemsList) ;
}
Void Delete_Command (Object Sender, DatalistCommandeventArgs E) {
// Retrieve the name of the item to remove. String item = ((label) E.Item.FindControl ("itemLabel")). Text;
// filter the cartview for the selected item and remove it from // The data source. CartView.rowfilter = "item = '" item "'"; if (cartview.count> 0) {CartView.delete (0) CartView.rowfilter = ""
// set the edititemindex property to -1 to exit editing mode. Be Sure // TO Rebind The Datalist to the data source to refresh the control.
Datalist itemlist = (data) sender; itemslist.edititemindex = -1; bindlist (itemslist);
}
Void Update_Command (Object Sender, DataListCommandeventArgs E) {
// Retrieve The Updated Values from The SELECTED ITEM. STRING ITEM = ((Label) E.Item.FindControl ("itemLabel")). Text; String Qty = ((TextBox) E.Item.FindControl ("QtyTextBox")) .Text; string price = ((TextBox) E.Item.FindControl ("PriceTextBox")). Text; // with a database, us, // because the data source in this example In-Memory // DataTable, Delete the Old Row and Replace It with a new one.
// filter the cartview for the selected item and remove it from // The data source. CartView.rowfilter = "item = '" item "'"; if (cartview.count> 0) {CartView.delete (0) CartView.rowfilter = ""
// ******************************************************** ************** // INSERT DATA VALIDATION CODE. Make Sure To Validate The User Before Converting To The Appropriate // Data Types and Updating The Data Source. // ******************************************************** ****************
// Add a new entry to replace the previous item. DATAROW DR = Cart.NewRow (); DR [0] = Qty; DR [1] = item; // if Necessary, Remove the '$' Character from the Price Before // Converting the price to a double. if (Price [0] == $ ') {DR [2] = Convert.TODOUBLE (price.substring (1));} else {DR [2] = Convert.Todouble (Price);} Cart.Rows.Add (DR);
// Set the EditItemIndex property to -1 to exit editing mode // Be sure to rebind the DataList to the data source to refresh // the control.DataList ItemsList = (DataList) sender;. ItemsList.EditItemIndex = -1; BindList ( Itemslist);
}
script> hEAD>