<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Editing Items with Validation in DataList</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<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 Titles where Type like '%cook%'";
Session [ "myTable" ] = ( fetchData ( query ) ).Tables [ 0 ];
bindList ( );
}
}
void myListEditHandler ( Object src, DataListCommandEventArgs e ) {
myList.EditItemIndex = e.Item.ItemIndex;
bindList ( );
}
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 pub_id = ( ( TextBox ) e.Item.FindControl ( "pub_id" ) ).Text;
float price = float.Parse ( ( ( TextBox ) e.Item.FindControl ( "price" ) ).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 [ "Pub_ID" ] = pub_id;
myRow [ "Price" ] = price;
myTable.AcceptChanges ( );
// refresh data in Session
Session [ "myTable" ] = myTable;
// set edit mode off
myList.EditItemIndex = -1;
bindList ( );
}
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>Editing Items with Validation in DataList</h2></div>
<hr size=1 width=92%>
<div align="center">
<form runat="server">
<asp:datalist id="myList" width=95% repeatcolumns=2 runat="server"
edititemstyle-backcolor="lightsteelblue"
onEditCommand="myListEditHandler"
onUpdateCommand="myListUpdateHandler"
onCancelCommand="myListCancelHandler">
<itemtemplate>
<table cellpadding=5>
<tr>
<td valign="top">
<img align="top" runat="server"
src='<%# Eval ( "title_id", "~/shared/images/title-{0}.gif" ) %>' >
</td>
<td valign="top">
<b>Title:</b> <%# Eval ( "title" ) %><br>
<b>Category:</b> <%# Eval ( "type" ) %><br>
<b>Publisher ID:</b> <%# Eval ( "pub_id" ) %><br>
<b>Price:</b><%# Eval ( "price", "{0:c}" ) %>
<p>
<asp:imagebutton commandname="edit" runat="server"
imageurl="~/shared/images/button_edit.gif" />
</td></tr>
</table>
</itemtemplate>
<edititemtemplate>
<table cellpadding=5>
<tr>
<td valign="top">
<img align="top" width=50 runat="server"
src='<%# Eval ( "title_id", "~/shared/images/title-{0}.gif" ) %>' >
</td>
<td valign="top">
<b>Title:</b> <%# Eval ( "title" ) %><br>
<b>Category:</b> <%# Eval ( "type" ) %><br>
<table cellpadding=0>
<col align="right">
<tr>
<td><b>Pub ID:</b> </td>
<td>
<asp:textbox id="pub_id" size=10 runat="server"
text='<%# Eval ( "pub_id" ) %>' />
<asp:requiredfieldvalidator runat="server"
controltovalidate="pub_id"
display="none"
errormessage="Publisher code cannot be blank." />
<asp:regularexpressionvalidator runat="server"
controltovalidate="pub_id"
validationexpression="^\d{4}$"
display="none"
errormessage="Publisher code must be a 4-digit string." />
</td>
</tr>
<tr>
<td><b>Price:</b> </td>
<td><asp:textbox id="price" size=10 runat="server"
text='<%# Eval ( "price" ) %>' />
<asp:requiredfieldvalidator runat="server"
controltovalidate="price"
display="none"
errormessage="Price cannot be blank." />
<asp:regularexpressionvalidator runat="server"
controltovalidate="price"
validationexpression="^\d*\.?\d*$"
display="none"
errormessage="Price must be in US currency format." />
</td>
</tr>
</table>
<p>
<asp:imagebutton commandname="update" runat="server"
imageurl="~/shared/images/button_update.gif" />
<asp:imagebutton commandname="cancel" runat="server"
imageurl="~/shared/images/button_cancel.gif"
causesvalidation=false />
</td></tr>
</table>
</edititemtemplate>
</asp:datalist>
<asp:validationsummary runat="server" displaymode="list"
showmessagebox showsummary=false />
</form>
</div>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>