Editing with a DataList

An DataList can be set up to operate in editing mode much like the Repeater. The major difference is in the fact that a DataList has an EditItemTemplate that is activated when a record is placed in edit mode. The following is one example of using a DataList for record editing. Like the Repeater, it is problematic in using a DataList for editing since the lack of a paging mechanism limits the DataList to editing databases of relatively modest sizes.

Note that making actual changes to the BooksDB.mdb database is not permitted in these tutorials; however, all other functions work as expected.

Book Edit

 
ID Type Title Author Description Price Qty Sale Edit
ID: DB111
Type: Database
Title: Oracle Database
Author: K. Loney
Description:
Get thorough coverage of Oracle Database 10g from the most comprehensive reference available, published by Oracle Press. With in-depth details on all the new features, this powerhouse resource provides an overview of database architecture and Oracle Grid Computing technology, and covers SQL, SQL*Plus, PL/SQL, dynamic PL/SQL, object-oriented features, and Java programming in the Oracle environment. You'll also find valuable database administration and application development techniques, plus an alphabetical reference covering major Oracle commands, keywords, features, and functions, with cross-referencing of topics.
Price: $69.99
Quantity: 10
Sale:
ID: DB222
Type: Database
Title: Databases in Depth
Author: C. J. Date
Description:
In Database in Depth, author and well-known database authority Chris Date lays out the fundamentals of the relational model. Don't let a lack to formal education in database theory hold you back. Instead, let Chris's clear explanation of relational concepts, set theory, the difference between model and implementation, relational algebra, normalization, and much more set you apart and well above the competition when it comes to getting work done with a relational database.
Price: $29.95
Quantity: 6
Sale:
ID: DB333
Type: Database
Title: Database Processing
Author: D. Kroenke
Description:
Revised to reflect the needs of today's users, this 10th edition of Database Processing assures that you will learn marketable skills. By presenting SQL SELECT statements near the beginning of the book readers will know early on how to query data and obtain results-seeing firsthand some of the ways that database technology is useful in the marketplace. By utilizing free software downloads, you will be able to actively use a DBMS product by the end of the 2nd chapter. Each topic appears in the context of accomplishing practical tasks. Its spiral approach to database design provides users with enhanced information not available in other database books on the market.
Price: $136.65
Quantity: 12
Sale:
ID: DB444
Type: Database
Title: Access Database Design
Author: S. Roman
Description:
When using software products with graphical interfaces, we frequently focus so much on the details of how to use the interface that we forget about the general concepts that allow us to understand and use the software effectively. This is particularly true of a powerful database product like Microsoft Access. Novice, and sometimes even experienced, programmers are so concerned with how something is done in Access that they often lose sight of the general principles that underlie their database applications. Access Database Design and Programming takes you behind the details of the Access interface, focusing on the general knowledge necessary for Access power users or developers to create effective database applications.
Price: $34.95
Quantity: 25
Sale:
ID: DB555
Type: Database
Title: SQL Server 2005
Author: P. Debetta
Description:
Get a developer-focused introduction to the new programmability features in the next version of Microsoft SQL Server-including integration with the Microsoft .NET Framework-and learn powerful new ways to manipulate your servers. Whether you're a developer currently working with T-SQL or Microsoft Visual Studio.NET, or you're responsible for database administration, you'll see how to draw from your existing skills and knowledge to exploit new SQL Server technology. With introductory-level code samples written in both T-SQL and C#, you'll understand how to take advantage of the cross-platform interoperability, native support for XML and Web services, shared language base, and other programming innovations to build better solutions from business intelligence to enterprise data management.
Price: $29.99
Quantity: 0
Sale:
ID: GR111
Type: Graphics
Title: Adobe Photoshop CS2
Author: Adobe
Description:
With this book, you learn by doing, getting your feet wet immediately as you progress through a series of hands-on projects that build on your growing Photoshop knowledge. Simple step-by-step instructions, review questions at the end of each chapter, and a companion CD with all of the book's project files make learning a breeze as the Adobe Creative Team takes you on a self-paced tour of the image-editing powerhouse. This bestselling guide has been completely revised to cover all of Photoshop CS2's new features and creative tools. This comprehensive guide starts with an introductory tour of the software and then progresses on through lessons on everything from Photoshop's interface to more complex topics like color management, Web graphics, and photo retouching.
Price: $29.99
Quantity: 4
Sale:
ID: GR222
Type: Graphics
Title: Learning Web Design
Author: J. Niederst
Description:
In Learning Web Design, author Jennifer Niederst shares the knowledge she's gained from years of web design experience, both as a designer and a teacher. This book starts from the beginning--defining the Internet, the Web, browsers, and URLs--so you don't need to have any previous knowledge about how the Web works. After reading this book, you'll have a solid foundation in HTML, graphics, and design principles that you can immediately put to use in creating effective web pages.
Price: $39.95
Quantity: 8
Sale:
ID: GR333
Type: Graphics
Title: Macromedia Flash Professional
Author: T. Green
Description:
Offering breakthrough video capabilities and powerful run-time effects, Macromedia Flash Professional 8 is poised to upend the Web video market, posing a serious challenge to Microsoft, RealNetworks, and Apple's efforts in the process. Here to make sure you're ready for it is the official Macromedia training for the program. Through 20-plus hours of project-based instruction, you'll learn how to program for the enhanced Flash Player; use ActionScript to create dynamic interactivity; take advantage of new Flash 8 features like the improved script editor, revamped library interface, and new Undo feature; and more. Simple step-by-step instructions peppered with plenty of visual aids and a CD that includes lesson files and a trial version of Flash Professional 8 leave you with a solid understanding of Flash development and the techniques required to tap your creative potential by producing dynamic, interactive content.
Price: $44.99
Quantity: 17
Sale:
ID: GR444
Type: Graphics
Title: Digital Photographer Handbook
Author: M. Freeman
Description:
Michael Freeman has a well-deserved reputation for effectively explaining the concepts behind digital picture-taking to a variety of audiences. Here, he turns his attention to the professionals and advanced hobbyists who are making the move from traditional to digital and want help mastering the technology and meeting their clients' new requirements. Freeman thoroughly answers the most frequently asked questions about the basics of digital capture, from cameras and computers to storage options, printers, and scanners. Photographers will learn the different file formats and how to save images for print or publishing on the web. They'll explore valuable software tools and basic image processing programs that fix common problems, and see how to improve pictures using an assortment of cropping and filtering techniques.
Price: $24.95
Quantity: 22
Sale:
ID: GR555
Type: Graphics
Title: Creating Motion Graphics
Author: T. Meyer
Description:
Trish and Chris Meyer share their years of real-world production experience in this vital update to the acclaimed Creating Motion Graphics with After Effects series. More than a step-by-step review of the features, you learn how this program integrates into real-world production workflows from award-winning artists who make their living using After Effects. This full-color guide is packed with visual examples, and the enclosed DVD-ROM is loaded with source material and projects that encourage you to practice their techniques.
Price: $59.95
Quantity: 13
Sale:
ID: HW111
Type: Hardware
Title: How Computers Work
Author: R. White
Description:
How Computers Work, 7th Edition, has been one of the bestselling computer books for the last 10 years. This four-color visual tutorial is a must-have for any computer user, from novice to expert. The rich graphics and intricate details about the inner workings of computers have been admired for years by consumers, instructors, professionals, and readers of all ages. A fresh cover and interior provide the reader with superior usability and it is the most aesthetically-pleasing edition yet!
Price: $29.99
Quantity: 8
Sale:
ID: HW222
Type: Hardware
Title: Upgrading and Repairing PCs
Author: S. Mueller
Description:
Push your PC's performance to the limit. Know the differences between: Pentium II, Pentium MMX, Pentium Pro, and earlier CPU chips and choose the best chip for your needs; understand compatibility and feature sets of processor upgrade sockets, motherboards, and chipsets; use Universal Serial Bus ports and devices to simplify peripheral installation, configuration, and improve performance; squeeze the most performance, life, and reliability out of your hard drives; prevent memory headaches: pick the right speed and type of SIMMs and DIMMs, run more programs at once, and work with bigger files; and integrate hot new hardware including 3D graphics accelerators, fast SDRAM memory, Zoomed Video and CardBus PC Cards for your notebook, and NLX motherboards with support for Single Edge Contact processors and Accelerated Graphics Ports.
Price: $59.99
Quantity: 5
Sale:
ID: HW333
Type: Hardware
Title: USB System Architecture
Author: D. Anderson
Description:
Universal Serial Bus System Architecture, Second Edition, based on the 2.0 version of the Universal Serial Bus specification, provides in-depth coverage and a detailed discussion of USB. It focuses on the USB protocol, signaling environment, and electrical specifications, along with the hardware/software interaction required to configure and access USB devices.
Price: $49.99
Quantity: 1
Sale:
ID: HW444
Type: Hardware
Title: Designing Embedded Hardware
Author: J. Catsoulis
Description:
Designing Embedded Hardware is a book about designing small machines for embedded applications. There are many books on the market dedicated to writing code for particular microprocessors, or that stress the philosophy of embedded system design without providing any practical information. This book steers a middle path, telling you what you need to know to create your own products, and distilling much of the lore of embedded systems design into a single volume. It shows you how to build a complete embedded system, add peripherals, and connect your system to other devices.
Price: $44.95
Quantity: 3
Sale:
ID: HW555
Type: Hardware
Title: Contemporary Logic Design
Author: R. Katz
Description:
Contemporary Logic Design introduces a wide range of software tools including schematic capture, logic simulation, Boolean minimization, multi-level minimization and state assignment. Links the traditional techniques of logic design (such as Karnaugh maps and breadboard techniques) with real-world design examples. Provides comprehensive, early coverage of programmable logic including ROMs, PALs, and PLAs. Includes a variety of examples, exercises, problems, and case studies that illustrate real design problems and challenge the reader to develop practical solutions using modern design tools.
Price: $102.95
Quantity: 2
Sale:
ID: SW111
Type: Software
Title: Java How to Program
Author: Deitel
Description:
This edition is completely up-to-date with The Java 2 Platform Standard Edition (J2SE) 1.5. Now includes topics such as autoboxing, enumerations, enhanced for loops, static import statements, variable-length argument lists, and much more. Presents each new concept in the context of a complete, working program, immediately followed by one or more windows showing the program's input/output dialog. A valuable reference for programmers and anyone interested in learning the Java programming language.
Price: $98.59
Quantity: 9
Sale:
ID: SW222
Type: Software
Title: C Programming Language
Author: B. Kernighan
Description:
The original authors of C and the first UNIX system present this concise and powerful guide to ANSI standard C programming. This version, building on Kerninghan and Ritchie's classic The C Programming Language, brings readers up-to-date with the finalized ANSI standard for C while teaching users how to take advantage of noted C features like economy of expression, its full set of operators and more. One reader claimed "Just about every C programmer I respect learned C from this book," while another raved th
Price: $44.25
Quantity: 12
Sale:
ID: SW333
Type: Software
Title: Programming C#
Author: J. Liberty
Description:
The programming language C# was built with the future of application development in mind. Pursuing that vision, C#'s designers succeeded in creating a safe, simple, component-based, high-performance language that works effectively with Microsoft's .NET Framework. Now the favored language among those programming for the Microsoft platform, C# continues to grow in popularity as more developers discover its strength and flexibility. And, from the start, C# developers have relied on Programming C# both as an introduction to the language and a means of further building their skills. The new fourth edition of Programming C#--the top-selling C# book on the market--has been updated to the C# ISO standard as well as changes to Microsoft's implementation of the language.
Price: $44.95
Quantity: 0
Sale:
ID: SW444
Type: Software
Title: Programming PHP
Author: R. J. Lerdorf
Description:
PHP is a simple yet powerful open source scripting language for creating dynamic web content. The millions of web sites powered by PHP are testament to its popularity and ease of use. PHP is used by both programmers, who appreciate its flexibility and speed, and web designers, who value its accessibility and convenience. Programming PHP is an authoritative guide to PHP 4, the latest version of the language, and is filled with the unique knowledge of the creator of PHP, Rasmus Lerdorf. This book explains PHP language syntax and programming techniques in a clear and concise manner, with numerous examples that illustrate both correct usage and common idioms. The book also includes style tips and practical programming advice that will help you become not just a PHP programmer, but a good PHP programmer.
Price: $39.95
Quantity: 17
Sale:
ID: SW555
Type: Software
Title: Visual Basic.NET Programming
Author: P. Vick
Description:
Visual Basic .NET builds on the legendary simplicity of Visual Basic to add the immense power of true object-oriented programming and Microsoft's .NET Framework. Now, for the first time, one of the language's architects has written a definitive technical reference and tutorial for Visual Basic .NET. Moving far beyond the online documentation, Paul Vick presents the language's complete specification, along with descriptions, reference materials, and code samples direct from the Visual Basic .NET design team.
Price: $49.99
Quantity: 13
Sale:
ID: SY111
Type: Systems
Title: Operating System Concepts
Author: A. Silberschatz
Description:
By staying current, remaining relevant, and adapting to emerging course needs, this text has continued to define the operating systems course. This Seventh Edition not only presents the latest and most relevant systems, it also digs deeper to uncover those fundamental concepts that have remained constant throughout the evolution of today's operating systems. With this strong conceptual foundation in place, students can more easily understand the details related to specific systems.
Price: $95.75
Quantity: 1
Sale:
ID: SY222
Type: Systems
Title: The UNIX Operating System
Author: J. D. Peek
Description:
Learning the Unix Operating System is a handy book for someone just starting with Unix or Linux, and it's an ideal primer for Mac and PC users of the Internet who need to know a little about Unix on the systems they visit. The fifth edition is the most effective introduction to Unix in print, covering Internet usage for email, file transfers, web browsing, and many major and minor updates to help the reader navigate the ever-expanding capabilities of the operating system.
Price: $19.95
Quantity: 12
Sale:
ID: SY333
Type: Systems
Title: Windows Server 2003
Author: W. R. Stanek
Description:
Here's the practical, pocket-sized reference for IT professionals supporting Windows .NET Server. Designed for quick referencing, this portable guide covers all the essentials for performing everyday system-administration tasks. Topics include managing workstations and servers, using Microsoft Active Directory services, creating and administering user and group accounts, managing files and directories, data security and auditing, data back-up and recovery, network administration using TCP/IP, WINS, and DNS, and more.
Price: $29.99
Quantity: 25
Sale:
ID: SY444
Type: Systems
Title: Linux in a Nutshell
Author: S. Figgins
Description:
Whether you're using Linux for personal software projects, for a small office or home office, to provide services to a small group of colleagues, or to administer a site responsible for millions of email and web connections each day, you need quick access to information on a wide range of tools. This book covers all aspects of administering and making effective use of Linux systems. Among its topics are booting, package management, and revision control. But foremost in Linux in a Nutshell are the utilities and commands that make Linux one of the most powerful and flexible systems available.
Price: $44.95
Quantity: 14
Sale:
ID: SY555
Type: Systems
Title: Mastering Active Directory
Author: R. R. King
Description:
Active Directory represents an enormous advance in network administration. It provides a vast set of powerful tools and technologies for managing a network within a native Windows environment. Mastering Active Directory for Windows Server 2003 is the resource you need to take full advantage of all it has to offer. You get a sound introduction to network directory services, then detailed, practical instruction in the work of implementing Active Directory and using all of its tools. This edition has been completely updated to address features new to Active Directory for Windows Server 2003.
Price: $49.99
Quantity: 8
Sale:
ID: WB111
Type: Web
Title: Ajax in Action
Author: D. Crane
Description:
Ajax in Action helps you implement that thinking--it explains how to distribute the application between the client and the server while retaining the integrity of the system. You will learn how to ensure your app is flexible and maintainable, and how good, structured design can help avoid problems like browser incompatibilities. Along the way it helps you unlearn many old coding habits. Above all, it opens your mind to the many advantages gained by placing much of the processing in the browser. If you are a web developer who has prior experience with web technologies, this book is for you.
Price: $22.67
Quantity: 14
Sale:
ID: WB222
Type: Web
Title: Professional ASP.NET 2.0
Author: B. Evjen
Description:
ASP.NET allows web sites to display unique pages for each visitor rather than show the same static HTML pages. The release of ASP.NET 2.0 is a revolutionary leap forward in the area of web application development. It brings with it a wealth of new and exciting built-in functions that reduce the amount of code you'll need to write for even the most common applications. With more than 50 new server controls, the number of classes inside ASP.NET 2.0 has more than doubled, and, in many cases, the changes in this new version are dramatic. This book will alert you to every new feature and capability that ASP.NET 2.0 provides so that you'll be prepared to put these new technologies into action.
Price: $32.99
Quantity: 21
Sale:
ID: WB333
Type: Web
Title: Cascading Style Sheets
Author: E. A. Meyer
Description:
This second edition of Cascading Style Sheets: The Definitive Guide completes the discussion of CSS2, explores CSS2.1, and introduces emerging elements of CSS3. Eric A. Meyer uses his trademark wit and humor to explore properties, tags, attributes, and implementation, as well as real-life issues, such as browser support and design guidelines. This book addresses experienced web authors and scripters, as well as novice authors who may be implementing CSS from scratch. Cascading Style Sheets: The Definitive Guide, Second Edition also includes a new foreword by Molly Holzschlag, a steering committee member for the Web Standards Project.
Price: $39.95
Quantity: 6
Sale:
ID: WB444
Type: Web
Title: DOM Scripting
Author: J. Keith
Description:
There are three main technologies married together to create usable, standards-compliant web designs: XHTML for data structure, Cascading Style Sheets for styling your data, and JavaScript for adding dynamic effects and manipulating structure on the fly using the Document Object Model. This book is about the latter of the three. DOM Scripting: Web Design with JavaScript and the Document Object Model gives you everything you need to start using JavaScript and the Document Object Model to enhance your web pages with client-side dynamic effects. Jermey starts off by giving you a basic crash course in JavaScript and the DOM, then moves on to provide you with several real world examples built up from scratch including dynamic image galleries and dynamic menus, and shows you how to manipulate web page style using the CSS DOM, and create markup on the fly.
Price: $23.09
Quantity: 8
Sale:
ID: WB555
Type: Web
Title: Microsoft ASP.NET 2.0
Author: D. Esposito
Description:
Get an expert, developer-focused introduction to the next big innovation in ASP.NET Web programming. Programming authority Dino Esposito takes you inside ASP.NET 2.0 technology, explaining how its updated, more powerful infrastructure allows you to create rich and dynamic Web applications quickly and with less code. Esposito examines how developer best practices with ASP.NET 1.x and classic ASP helped drive the architectural changes in ASP.NET 2.0, and he offers prerelease insights into its final architecture. With concise concept and feature explanations and more than 70 fully functional examples, this comprehensive introduction gives you everything you need to dig into ASP.NET 2.0 right now.
Price: $29.99
Quantity: 12
Sale:

Figure 9-27. Editing a database with a DataList.

The DataList displays all fields in all Books records of the BooksDB.mdb database. The header row displays blank input areas along with an "Insert" button for creating a new product record. Item rows display records with "Edit" and "Delete" buttons. The "Edit" button switches the fields to input areas for changing records and displays "Update" and "Cancel" buttons. The "Delete" button removes the record from the table.

Coding the DataList

Code for the DataList is shown below along with its AccessDataSource. A second AccessDataSource is needed to populate DropDownLists of book types during record insertion and updating.

<asp:AccessDataSource id="BookSource" Runat="Server"
  DataFile="../Databases/BooksDB.mdb"
  SelectCommand = "SELECT * FROM Books ORDER BY BookID"/>

<asp:AccessDataSource id="TypeSource" Runat="Server"
  DataFile="../Databases/BooksDB.mdb"
  SelectCommand = "SELECT DISTINCT BookType FROM Books ORDER BY BookType"/>

<h3>Book Edit</h3>
	
<asp:Label id="EditMessage" Text=" " ForeColor="#FF0000" 
EnableViewState="False" Runat="Server"/>

<asp:DataList id="DataListEdit" DataSourceID="BookSource" Runat="Server"
  OnItemCommand="Edit_Record"
  CellSpacing="0"
  CellPadding="3"
  RepeatColumns="3"
  RepeatDirection="Horizontal"
  GridLines="Both"
  BorderWidth="1">
  
  <HeaderTemplate>
  <table id="Add"  border="1">
  <tr>
    <th>ID</th>
    <th>Type</th>
    <th>Title</th>
    <th>Author</th>
    <th>Description</th>
    <th>Price</th>
    <th>Qty</th>
    <th>Sale</th>
    <th>Edit</th>
  </tr>
  <tr>
    <td><asp:TextBox id="BookID" Runat="Server"
          Font-Size="8pt" Width="45px"/></td>
    <td><asp:DropDownList id="BookType"
          DataSourceID="TypeSource" Runat="Server"
          DataTextField="BookType"
          DataValueField="BookType"
          Font-Size="8pt"/></td>
    <td><asp:TextBox id="BookTitle" Runat="Server"
          Font-Size="8pt" Width="80px"/></td>
    <td><asp:TextBox id="BookAuthor" Runat="Server"
          Font-Size="8pt" Width="90px"/></td>
    <td><asp:TextBox id="BookDescription" Runat="Server"
          TextMode="MultiLine" Width="150" Rows="2"
          Font-Name="Arial" Font-Size="7pt"/></td>
    <td><asp:TextBox id="BookPrice" Runat="Server"
          Font-Size="8pt" Width="50px"
          Style="text-align:right"/></td>
    <td><asp:TextBox id="BookQty" Runat="Server"
          Font-Size="8pt" Width="25px"
          Style="text-align:right"/></td>
    <td><asp:CheckBox id="BookSale" Runat="Server"/></td>
    <td nowrap>
        <asp:Button Text="Insert" CommandName="Insert" Runat="Server"
          Font-Size="7pt" Width="45"/></td>
  </tr>
  <tr>
    <td colspan="9" style="border-bottom:solid 1 black"></td>
  </tr>	
  </table>
  </HeaderTemplate>
  
  <ItemTemplate>
  <table id="Display" border="0" cellpadding="1">
  <tr>
    <th><b>ID: </b></th>
    <td><asp:Label id="BookIDDelete" Runat="Server"
          Text='<%# Eval("BookID") %>'/></td>
  </tr>
  <tr>
    <th><b>Type: </b></th>
    <td><asp:Label Runat="Server"
          Text='<%# Eval("BookType") %>'/></td>
  </tr>
  <tr>
    <th><b>Title: </b></th>
    <td><asp:Label Runat="Server"
          Text='<%# Eval("BookTitle") %>'/></td>
  </tr>
  <tr>
    <th><b>Author: </b></th>
    <td><asp:Label Runat="Server"
          Text='<%# Eval("BookAuthor") %>'/></td>
  </tr>
  <tr style="vertical-align:top">
    <th><b>Description: </b></th>
    <td><asp:Panel Width="150" Height="100" ScrollBars="Vertical" 
        Runat="Server">
          <asp:Label Runat="Server"
          Text='<%# Eval("BookDescription") %>'/>
      </asp:Panel></td>
  <tr>
  <th><b>Price: </b></th>
    <td><asp:Label Runat="Server"
          Text='<%# Eval("BookPrice") %>'/></td>
  </tr>
  <tr>
    <th><b>Quantity: </b></th>
    <td><asp:Label Runat="Server"
          Text='<%# Eval("BookQty") %>'/></td>
  </tr>
  <tr>
    <th><b>Sale: </b></th>
    <td><asp:CheckBox Runat="Server"
          Checked='<%# Eval("BookSale") %>'/></td>
  </tr>
  </table>
  <asp:Button Text="Edit" CommandName="Edit" Runat="Server"
    Font-Size="7pt" Width="45"/>
  <asp:Button Text="Delete" CommandName="Delete" Runat="Server"
    Font-Size="7pt" Width="45"/>
  </ItemTemplate>
  
  <EditItemTemplate>
  <table id="Edit" border="0" cellpadding="0">
  <tr>
    <th>ID:</th>
    <td><asp:TextBox id="BookID" Runat="Server" ReadOnly="True"
          Text='<%# Eval("BookID") %>'
          Font-Size="8pt" Width="45px"/></td>
  </tr>
  <tr>
    <th>Type:</th>
    <td><asp:DropDownList id="BookType" Runat="Server"
          DataSourceID="TypeSource"
          DataTextField="BookType"
          DataValueField="BookType"
          SelectedValue='<%# Eval("BookType") %>'
          Font-Size="8pt"/></td>
  </tr>
  <tr>
    <th>Title:</th>
    <td><asp:TextBox id="BookTitle" Runat="Server"
          Text='<%# Eval("BookTitle") %>'
          Font-Size="8pt" Width="145px"/></td>
  </tr>
  <tr>
    <th>Author:</th>
    <td><asp:TextBox id="BookAuthor" Runat="Server"
          Text='<%# Eval("BookAuthor") %>'
          Font-Size="8pt" Width="145px"/></td>
  </tr>
  <tr style="vertical-align:top">
    <th>Description:</th>
    <td><asp:TextBox id="BookDescription" Runat="Server"
          Text='<%# Eval("BookDescription") %>'
          TextMode="MultiLine"
          Font-Name="Arial" Font-Size="8pt" 
          Width="145" Rows="5"/></td>
  <tr>
    <th>Price:</th>
    <td><asp:TextBox id="BookPrice" Runat="Server"
          Text='<%# Eval("BookPRice") %>'
          Font-Size="8pt" Width="50px"
          Style="text-align:right"/></td>
  </tr>
  <tr>
    <th>Qty:</th>
    <td><asp:TextBox id="BookQty" Runat="Server"
          Text='<%# Eval("BookQty") %>'
          Font-Size="8pt" Width="25px" 
          Style="text-align:right"/></td>
  </tr>
  <tr>
    <th>Sale:</th>
    <td><asp:CheckBox id="BookSale" Runat="Server"
          Checked='<%# Eval("BookSale") %>'/></td>
  </tr>
  </table>
  <asp:Button Text="Update" CommandName="Update" Runat="Server"
    Font-Size="7pt" Width="45"/>
  <asp:Button Text="Cancel" CommandName="Cancel" Runat="Server"
    Font-Size="7pt" Width="45"/>
  </EditItemTemplate>

</asp:DataList>
Listing 9-58. Code for editing with a DataList.

As in the case with the Repeater, data binding expressions are in the format <%# Eval("field") %>. It is not necessary to use the Bind() method that is needed when working with a GridView, DetailsView, or FormView.

Like a Repeater, a DataList recognizes an ItemCommand event when an enclosed command button is clicked. Its OnItemCommand event handler calls a subprogram for this event. The DataList also recognizes OnItemEdit, OnItemUpdate, OnItemDelete, and OnItemCancel events for scripting them separately. In this example, the general-purpose OnItemCommand handler calls the Edit_Record subprogram to handle all command events.

A <HeaderTemplate> is defined to contain the input areas for adding a new record. It is formatted as a table to control formatting of the items. An insert button is defined as a command button with the CommandName "Insert".

The <ItemTemplate>, as well, is formatted as a table to align items appearing in the cells. Each record has accompanying edit and delete command buttons with "Edit" and "Delete" command names that call the Edit_Record subprogram through the OnItemCommand event handler. The edit button causes display of the <EditItemTemplate> and associated "Update" and "Cancel" buttons for updating the record.

DataList Editing

The script for the DataList is similar to the one used for the previous Repeater. The Edit_Record subprogram, being called from a DataList, has the argument DataListCommandEventArgs. The subprogram's first task is to locate controls for the selected record in the same manner as is done for the Repeater. These data input controls appear in the HeaderTemplate and EditItemTemplate when the "Edit" button is clicked. Then the subprogram performs activities depending on the CommandName of the button that is clicked.

Sub Edit_Record (Src As Object, Args As DataListCommandEventArgs)
  
  Dim BookID As TextBox = Args.Item.FindControl("BookID")
  Dim BookType As DropDownList = Args.Item.FindControl("BookType")
  Dim BookTitle As TextBox = Args.Item.FindControl("BookTitle")
  Dim BookAuthor As TextBox = Args.Item.FindControl("BookAuthor")
  Dim BookDescription As TextBox = Args.Item.FindControl("BookDescription")
  Dim BookPrice As TextBox = Args.Item.FindControl("BookPrice")
  Dim BookQty As TextBox = Args.Item.FindControl("BookQty")
  Dim BookSale As CheckBox = Args.Item.FindControl("BookSale")
  
  If Args.CommandName = "Edit" Then
    
    DataListEdit.EditItemIndex = Args.Item.ItemIndex
    DataListEdit.DataBind()
  
  End If
  
  If Args.CommandName = "Cancel" Then
    
    ' Set the EditItemIndex property to -1 to exit editing mode. Be sure
    ' to rebind the DataList to the data source to refresh the control.
    DataListEdit.EditItemIndex = -1
    DataListEdit.DataBind()
    
  End If
  
  If Args.CommandName = "Insert" Then
    
    Dim SQLString As String = "INSERT INTO Books " & _
      "(BookID, BookType, BookTitle, BookAuthor, BookDescription, " & _
      "BookPrice, BookQty, BookSale) VALUES (" & _
      "'" & BookID.Text            & "', " & _
      "'" & BookType.SelectedValue & "', " & _
      "'" & BookTitle.Text         & "', " & _
      "'" & BookAuthor.Text        & "', " & _
      "'" & BookDescription.Text   & "', " & _
      "'" & BookPrice.Text         & "', " & _
      "'" & BookQty.Text           & "', " & _
      BookSale.Checked             & ")"
    BookSource.InsertCommand = SQLString
    BookSource.Insert()
    EditMessage.Text = "Record " & BookID.Text & " inserted"
    
  End If
  
  If Args.CommandName = "Update" Then
    
    Dim SQLString As String = "UPDATE Books SET " & _
      "BookType = '"         & BookType.SelectedValue & "', " & _
      "BookTitle = '"        & BookTitle.Text         & "', " & _
      "BookAuthor = '"       & BookAuthor.Text        & "', " & _
      "BookDescription = '"  & BookDescription.Text   & "', " & _
      "BookPrice = '"        & BookPrice.Text         & "', " & _
      "BookQty = '"          & BookQty.Text           & "', " & _
      "BookSale = "          & BookSale.Checked       & " "   & _
      "WHERE BookID = '" & BookID.Text & "'"
    BookSource.UpdateCommand = SQLString
    BookSource.Update()
    DataListEdit.EditItemIndex = -1
    DataListEdit.DataBind()
    EditMessage.Text = "Record " & BookID.Text & " updated"
    
  End If
  
  If Args.CommandName = "Delete" Then
    
    Dim BookIDDelete As Label = Args.Item.FindControl("BookIDDelete")
    Dim SQLString As String = "DELETE FROM Books " & _
                               "WHERE BookID = '" & BookIDDelete.Text & "'"
    BookSource.DeleteCommand = SQLString
    BookSource.Delete()
    EditMessage.Text = "Record " & BookIDDelete.Text & " deleted"
    
  End If

End Sub
Listing 9-59. Script for DataList editing.

When an "Edit" button is clicked, a record's EditItemTemplate is revealed as a set of input controls for changing data values. This switch from the ItemTemplate is activated by setting the EditItemIndex property of the record. This portion of the script is repeated below.

If Args.CommandName = "Edit" Then

  DataListEdit.EditItemIndex = Args.Item.ItemIndex
  DataListEdit.DataBind()

End If
Listing 9-60. Switching a DataList row to edit mode.

The current ItemIndex of the selected item is the DataList's index number for the record, numbered beginning with 0 for the HeaderTemplate. This index number is passed to the subprogram through its argument list. It identifies the ItemTemplate that should be replaced by its EditItemTemplate by setting the DataList's EditItemIndex to the same value. Then the DataList is re-bound to its AccessDataSource.

The reverse process is following when the "Cancel" button is clicked. The currently displayed EditItemTemplate is replaced by its ItemTemplate. Setting the EditItemIndex to -1 displays the ItemTemplate for any item currently in edit mode.

If Args.CommandName = "Cancel" Then

  DataListEdit.EditItemIndex = -1
  DataListEdit.DataBind()

End If
Listing 9-61. Switching a DataList row to display mode.

The remainder of the scripts are virtually the same as for the Repeater when the "Insert," "Update," and "Delete" buttons are clicked. One difference is in the delete routine. Deletion takes place with the ItemTemplate displayed. Within this template, the BookID required to identify the record is displayed in a Label control, not with a TextBox as occurs when the EditItemTemplate is being displayed. Therefore, the FindControl() method must find a Label.

If Args.CommandName = "Delete" Then
  
  Dim BookIDDelete As Label = Args.Item.FindControl("BookIDDelete")
  Dim SQLString As String = "DELETE FROM Books " & _
                            "WHERE BookID = '" & BookIDDelete.Text & "'"
  BookSource.DeleteCommand = SQLString
  BookSource.Delete()
  EditMessage.Text = "Record " & BookIDDelete.Text & " deleted"
  
End If
Listing 9-62. Finding a control in a DataList row.

This Edit_Record subprogram does not perform data validation. Routines can be easily added as described for the Repeater.