<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>DataList Editing Command Events Example</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<style>
<!--
table.editor {
font-size:8pt}
table.editor th {
background-color:tan; padding:5}
table.editor td {
background-color:palegoldenrod; vertical-align:top}
-->
</style>
<script language="C#" runat="server" src="fetchData_sql.cs" />
<script language="C#" runat="server">
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) {
// fetch and store data into Session
string query = "SELECT * FROM Suppliers";
Session [ "myTable" ] = ( fetchData ( query ) ).Tables [ 0 ];
bindList ( );
}
}
void myListEditHandler ( Object src, DataListCommandEventArgs e ) {
myList.EditItemIndex = e.Item.ItemIndex;
bindList ( );
// clear optional message
msg.Text = "";
}
void myListCancelHandler ( Object src, DataListCommandEventArgs e ) {
myList.EditItemIndex = -1;
bindList ( );
}
void myListUpdateHandler ( Object src, DataListCommandEventArgs e ) {
// e.Item represents the current row in edit mode ...
// get updated row values
string contact = ( ( TextBox ) e.Item.FindControl ( "contact" ) ).Text;
string title = ( ( TextBox ) e.Item.FindControl ( "title" ) ).Text;
string address = ( ( TextBox ) e.Item.FindControl ( "address" ) ).Text;
string city = ( ( TextBox ) e.Item.FindControl ( "city" ) ).Text;
string phone = ( ( TextBox ) e.Item.FindControl ( "phone" ) ).Text;
// load data from Session
DataTable myTable = ( DataTable ) Session [ "myTable" ];
// for demo purposes, the following procedure updates the row
// only in the datatable bound to the list, not in the database ...
// in actual practice, though, you can instead
// call a method to update the row in the database here ...
// get and update row
DataRow myRow = myTable.Rows [ e.Item.ItemIndex ];
myRow [ "ContactName" ] = contact;
myRow [ "ContactTitle" ] = title;
myRow [ "Address" ] = address;
myRow [ "City" ] = city;
myRow [ "Phone" ] = phone;
myTable.AcceptChanges ( );
// refresh data in Session
Session [ "myTable" ] = myTable;
// set edit mode off
myList.EditItemIndex = -1;
bindList ( );
// optional message
msg.Text = "Data for <b>" +
( ( Label ) e.Item.FindControl ( "company" ) ).Text +
"</b> updated";
}
void bindList ( ) {
// bind list to current data in Session
myList.DataSource = Session [ "myTable" ];
myList.DataBind ( );
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>DataList Editing Command Events Example</h2></div>
<hr size=1 width=92%>
<div align="center">
<form runat="server">
<p><asp:label id="msg" runat="server" /></p>
<asp:datalist id="myList" runat="server"
width="92%" cellpadding=5 repeatcolumns=2
backcolor="ghostwhite"
onEditCommand="myListEditHandler"
onUpdateCommand="myListUpdateHandler"
onCancelCommand="myListCancelHandler">
<headertemplate>
NorthWind Traders Suppliers
</headertemplate>
<headerstyle backcolor="darkslategray"
forecolor="khaki" font-bold />
<itemtemplate>
<asp:linkbutton runat="server" commandname="edit"
text='<%# Eval ( "CompanyName" ) %>' />
</itemtemplate>
<itemstyle font-size="9pt" verticalalign="top" />
<edititemtemplate>
<table cellspacing=1 cellpadding=1 class="editor" width=100% align="center">
<tr>
<th colspan=2><asp:label id="company" runat="server"
text='<%# Eval ( "CompanyName" ) %>' /></th>
</tr>
<tr>
<td>Contact: </td>
<td><asp:textbox id="contact" runat="server"
text='<%# Eval ( "ContactName" ) %>' /></td>
</tr>
<tr>
<td>Position: </td>
<td><asp:textbox id="title" runat="server"
text='<%# Eval ( "ContactTitle" ) %>' /></td>
</tr>
<tr>
<td>Address: </td>
<td><asp:textbox id="address" runat="server"
text='<%# Eval ( "Address" ) %>' /></td>
</tr>
<tr>
<td>City: </td>
<td><asp:textbox id="city" runat="server"
text='<%# Eval ( "City" ) %>' /></td>
</tr>
<tr>
<td>Phone: </td>
<td><asp:textbox id="phone" runat="server"
text='<%# Eval ( "Phone" ) %>' /></td>
</tr>
<tr align="center">
<td colspan=2>
<asp:linkbutton commandname="update" runat="server" text="Update" />
<asp:linkbutton commandname="cancel" runat="server" text="Cancel" />
</td>
</tr>
</table>
</edititemtemplate>
</asp:datalist>
</form>
</div>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc -->
</body>
</html>