The DataList, with its ItemTemplate and EditItemTemplate, make it very easy for you to control the appearance (and screen real estate) of the data. As I said before, it requires more coding but the results may well be worth the effort.
In this article and example program we will deal with the Northwind Customers table. I have included nine columns of editable data. I have divided the work between an aspx page and a code-behind page. In the aspx page we layout our presentation of data, while the code-behind file places the DataList in edit mode, and handles the updating of modified data. The aspx file will be shown below in several sections to make it easier to explain what each section does. This first section is the usual top-of-page "stuff" and the definition of the DataList Control. The only items of note are that we have set the OnEditCommand, OnUpdateCommand, and OnCancelCommand properties to the names of the corresponding event handlers which are defined in the code-behind file.
|
<%@ Page Language="vb" Src="DataListEdit.aspx.vb" Inherits="Main" %> <html> <head> <title>DataList Edit</title> <style rel="stylesheet"> .customers { font: 9pt Verdana, Arial, sans-serif; } .customersHead { font: bold 8pt Verdana, Arial, sans-serif; background-color:#4A3C8C; color:white; } a { text-decoration:underline; } a:hover { text-decoration:underline; color:#4A3C8C; } </style> </head> <body> <form runat="server" ID="Form1"> <div align="center"> <h3>Customers Table</h3> </div> <asp:DataList id="dtlcustomers" runat="server" width="760" BorderWidth="1" HeaderStyle-CssClass="customersHead" AlternatingItemStyle-BackColor="#DEDFDE" Font-Size="10" Align="Center" OnEditCommand="dtlcustomers_Edit" OnUpdateCommand="dtlcustomers_Update" OnCancelCommand="dtlcustomers_Cancel"> |
|
<ItemTemplate> <table cellpadding="2" cellspacing="0" width="100%"> <tr> <td colspan="4" class="customersHead"> <h3><%# DataBinder.Eval(Container.DataItem, "CompanyName") ></h3> </td> </tr> <tr> <td Width="100%" Align="left" colspan="4"> <asp:button id="btnEdit" Runat="server" CommandName="edit" Text="Edit" /> </td> </tr> <tr> <td Width="25%" Align="left"> <b>Contact Name</b> </td> <td Width="25%" Align="left"> <%# DataBinder.Eval(Container.DataItem, "ContactName") %> </td> <td Width="25%" Align="left"> <b>Contact Title</b> </td> <td Width="25%" Align="left"> <%# DataBinder.Eval(Container.DataItem, "ContactTitle") %> </td> </tr> <tr> <td Width="25%" Align="left"> <b>Address</b> </td> <td Width="25%" Align="left"> <%# DataBinder.Eval(Container.DataItem, "Address") %> </td> <td Width="25%" Align="left"> <b>City</b> </td> <td width="25%" align="left"> <%# DataBinder.Eval(Container.DataItem, "City") %> </td> </tr> <tr> <td Width="25%" Align="left"> <b>Postal Code</b> </td> <td Width="25%" Align="left"> <%# DataBinder.Eval(Container.DataItem, "PostalCode") %> </td> <td Width="25%" Align="left"> <b>Country</b> </td> <td width="25%" align="left"> <%# DataBinder.Eval(Container.DataItem, "Country") %> </td> </tr> <tr> <td Width="25%" Align="left"> <b>Phone</b> </td> <td Width="25%" Align="left"> <%# DataBinder.Eval(Container.DataItem, "Phone") %>> </td> <td Width="25%" Align="left"> <b>Fax</b> </td> <td width="25%" align="left"> <%# DataBinder.Eval(Container.DataItem, "Fax") %> </td> </tr> </Table> </ItemTemplate> |
|
<EditItemTemplate> <table cellpadding="2" cellspacing="0" width="100%"> <tr> <td colspan="2" class="customersHead"> <h3><%# DataBinder.Eval(Container.DataItem, "CompanyName") %></h3> </td> </tr> <tr> <td Width="50%" Align="Left"> <b>Company Name</b> </td> <td Width="50%" Align="left"> <asp:TextBox id="txtCompanyName" runat="server" MaxLength="40" Columns="40" Text='<%# DataBinder.Eval(Container.DataItem, "CompanyName") %>'/> </td> </tr> <tr> <td Width="50%" Align="Left"> <b>Contact Name</b> </td> <td Width="50%" Align="left"> <asp:TextBox id="txtContactName" Runat="server" MaxLength="30" Columns="30" Text='<%# DataBinder.Eval(Container.DataItem, "ContactName") %>'/> </td> </tr> <tr> <td Width="50%" Align="Left"> <b>Contact Title</b> </td> <td Width="50%" Align="left"> <asp:TextBox id="txtContactTitle" Runat="server" MaxLength="30" Columns="30" Text='<%# DataBinder.Eval(Container.DataItem, "ContactTitle") %>'/> </td> </tr> <tr> <td Width="50%" Align="Left"> <b>Address</b> </td> <td Width="50%" Align="left"> <asp:TextBox id="txtAddress" Runat="server" MaxLength="60" Columns="60" Text='<%# DataBinder.Eval(Container.DataItem, "Address") %>'/> </td> </tr> <tr> <td Width="50%" Align="Left"> <b>City</b> </td> <td Width="50%" Align="left"> <asp:TextBox id="txtCity" Runat="server" MaxLength="15" Columns="15" Text='<%# DataBinder.Eval(Container.DataItem, "City") %>'/> </td> </tr> <tr> <td Width="50%" Align="Left"> <b>Postal Code</b> </td> <td Width="50%" Align="left"> <asp:TextBox id="txtPostalCode" Runat="server" MaxLength="10" Columns="10" Text='<%# DataBinder.Eval(Container.DataItem, "PostalCode") %>'/> </td> </tr> <tr> <td Width="50%" Align="Left"> <b>Country</b> </td> <td Width="50%" Align="left"> <asp:TextBox id="txtCountry" Runat="server" MaxLength="15" Columns="15" Text='<%# DataBinder.Eval(Container.DataItem, "Country") %>'/> </td> </tr> <tr> <td Width="50%" Align="Left"> <b>Phone</b> </td> <td Width="50%" Align="left"> <asp:TextBox id="txtPhone" Runat="server" MaxLength="24" Columns="24" Text='<%# DataBinder.Eval(Container.DataItem, "Phone") %>'/> </td> </tr> <tr> <td Width="50%" Align="Left"> <b>Fax</b> </td> <td Width="50%" Align="left"> <asp:TextBox id="txtFax" Runat="server" MaxLength="24" Columns="24" Text='<%# DataBinder.Eval(Container.DataItem, "Fax") %>'/> </td> </tr> <tr> <td colspan="2"> <asp:Label id="lblCustomerID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CustomerID") %>' Visible="false" /> </td> </tr> <tr> <td Width="50%" Align="right"> <asp:Button id="btnUpdate" Runat="server" CommandName="update" Text="Update" /> <asp:Button id="btnCancel" Runat="server" CommandName="cancel" Text="Cancel" /> </td> <td Width="50%" Align="Left"> </td> </tr> </table> </EditItemTemplate> </asp:DataList> </form> </body> </html> |
|
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Configuration Public Class Main : Inherits Page Private strConn As String = ConfigurationSettings.AppSettings("ConnectionString") Public dtlCustomers As DataList Public Sub Page_Load(sender as Object, e as EventArgs) If Not IsPostBack Then BindTheData() End If End Sub Private Sub BindTheData() Dim objConn as new SqlConnection(strConn) Dim strSQL as String strSQL = "SELECT Top 5 * FROM Customers" Dim sda as new SqlDataAdapter(strSQL, objConn) Dim ds as new DataSet() sda.Fill(ds,"Customers") dtlCustomers.DataSource = ds.Tables("Customers").DefaultView dtlCustomers.DataBind() End Sub |
|
Public Sub dtlCustomers_Edit(sender as Object, e as DataListCommandEventArgs) dtlCustomers.EditItemIndex = e.Item.ItemIndex BindTheData() End Sub Public Sub dtlCustomers_Cancel(sender as Object, e as DataListCommandEventArgs) dtlCustomers.EditItemIndex = -1 BindTheData() End Sub |
|
Public Sub dtlCustomers_Update(sender As Object, e As DataListCommandEventArgs) Dim strCompanyName, strContactName, strContactTitle, strCustomerID As String Dim strAddress, strCity, strPostalCode, strCountry, strPhone, strFax As String strCompanyName = CType(e.Item.FindControl("txtCompanyName"), TextBox).Text strContactName = CType(e.Item.FindControl("txtContactName"), TextBox).Text strContactTitle = CType(e.Item.FindControl("txtContactTitle"), TextBox).Text strAddress = CType(e.Item.FindControl("txtAddress"), TextBox).Text strCity = CType(e.Item.FindControl("txtCity"), TextBox).Text strPostalCode = CType(e.Item.FindControl("txtPostalCode"),TextBox).Text strCountry = CType(e.Item.FindControl("txtCountry"),TextBox).Text strPhone = CType(e.Item.FindControl("txtPhone"),TextBox).Text strFax = CType(e.Item.FindControl("txtFax"),TextBox).Text strCustomerID = CType(e.Item.FindControl("lblCustomerID"), Label).Text Dim strSQL As String strSQL = "Update Customers " _ & "Set CompanyName = @CompanyName," _ & "ContactName = @ContactName," _ & "ContactTitle = @ContactTitle, " _ & "Address = @Address, " _ & "City = @City, " _ & "PostalCode = @PostalCode, " _ & "Country = @Country, " _ & "Phone = @Phone, " _ & "Fax = @Fax " _ & "WHERE CustomerID = @CustomerID" Dim objConn As New SqlConnection(strConn) Dim cmdSQL As New SqlCommand(strSQL, objConn) cmdSQL.Parameters.Add(new SqlParameter("@CompanyName", SqlDbType.NVarChar, 40)) cmdSQL.Parameters("@CompanyName").Value = strCompanyName cmdSQL.Parameters.Add(new SqlParameter("@ContactName", SqlDbType.NVarChar, 30)) cmdSQL.Parameters("@ContactName").Value = strContactName cmdSQL.Parameters.Add(new SqlParameter("@ContactTitle", SqlDbType.NVarChar, 30)) cmdSQL.Parameters("@ContactTitle").Value = strContactTitle cmdSQL.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 60)) cmdSQL.Parameters("@Address").Value = strAddress cmdSQL.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 15)) cmdSQL.Parameters("@City").Value = strCity cmdSQL.Parameters.Add(new SqlParameter("@PostalCode", SqlDbType.NVarChar, 10)) cmdSQL.Parameters("@PostalCode").Value = strPostalCode cmdSQL.Parameters.Add(new SqlParameter("@Country", SqlDbType.NVarChar, 15)) cmdSQL.Parameters("@Country").Value = strCountry cmdSQL.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NVarChar, 24)) cmdSQL.Parameters("@Phone").Value = strPhone cmdSQL.Parameters.Add(new SqlParameter("@Fax", SqlDbType.NVarChar, 24)) cmdSQL.Parameters("@Fax").Value = strFax cmdSQL.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.NChar, 5)) cmdSQL.Parameters("@CustomerID").Value = strCustomerID objConn.Open() cmdSQL.ExecuteNonQuery() objConn.Close() dtlCustomers.EditItemIndex = -1 BindTheData() End Sub End Class |
You may download the code here.