Update Multiple Records with the DataGrid, DataList and Repeater

by Lee Chen

.NET provides us with rich data controls to update a database. Both the DataGrid and DataList controls have built-in support for updating data with EditItemTemplate. These controls make the process easy, powerful, and smooth. However, the .NET data controls' built-in database updating is a single-row-based updating. You update only one record at once, and the Repeater provides no updating capability at all. In our daily programming practice, we need a function to update more than one record at a time.

Here, I will show you a very simple way to update multiple data records using Repeater, DataList or DataGrid controls.

Let's take Repeater as an example first.

I here use SQL Server sample database Pubs as our database. Two tables will be involved: Titles and Publishers. First, I create four Stored Procedures:

  1. BookInformationUpdate -- displays book information on web;
  2. BookInformationUpdate -- updates book information;
  3. BookPublisher -- gets book publisher name, and displays them in a DropDownList control for update;
  4. BookType -- gets distinct book types, and displays them in a DropDownList control for update;

Then, create a web form to display and update book information:

<%@ Page Language="C#" %>

<%@ import Namespace = "UpdateRecord" %>
<%@ import Namespace = "System.Data" %>
<%@ import Namespace = "System.Data.SqlClient" %>

<script runat="server">

//Page load to call function getBookInformation
void Page_Load(Object send, EventArgs E){
  if (! IsPostBack){

//Bind book information data to a Repeater control
void getBookInformation(){
    pubDB getBooks = new pubDB();
    repeaterBooks.DataSource = getBooks.GetBooksTitle();

void btnSubmit_Click(Object send, EventArgs E){
   //Update code will be here	

//Function to display book type in a DropDownList
DataSet getBookType(){
    pubDB getBooks = new pubDB();
	return getBooks.GetBooksType();

//Function to set a selected index for individual book's tpye
int getBookTypeIndex(string bookType){
    pubDB getBooks = new pubDB();
    return getBooks.getIndexForType(bookType.ToString().Trim());

//Function to display book publisher in a DropDownList
DataSet getBookPublisher(){
    pubDB getBooks = new pubDB();
    return getBooks.GetBooksPublisher();

//Function to set a selected index for individual book's publisher
int getBookPublisherIndex(string bookPub){
    pubDB getBooks = new pubDB();
    return getBooks.getIndexForPublisher(bookPub.ToString().Trim());


<form runat="server">
<table width="100%" border="0">

    <td colspan="4" align="center">
      <font face="Arial" size="2">

      <b>Update Multiple Records with Repeater, DataList, or DataGrid</b>
  <asp:Repeater ID="repeaterBooks" runat="server">

      <table align="center" width="100%" border="0" cellpadding="2"

      <tr bgcolor="#E17546">
        <td width="40%"><font face="Arial" size="2"
          color="#FFFFFF"><b>Book Title</b></font></td>

        <td width="20%"><font face="Arial" size="2"
          color="#FFFFFF"><b>Book Type</b></font></td>

        <td width="20%"><font face="Arial" size="2"
          color="#FFFFFF"><b>Book Price</b></font></td>

        <td width="20%"><font face="Arial" size="2"
          color="#FFFFFF"><b>Book Publisher</b></font></td>

      <tr bgcolor="#F9FFDF">
        <td width="40%" valign="top">

          <font face="Arial" size="2">
          <%# DataBinder.Eval(Container.DataItem,"Title").ToString() %>
        <td width="20%" valign="top">

          <asp:DropDownList ID="ddlBookType" DataValueField="Type"
            DataSource='<%# getBookType() %>'
            SelectedIndex='<%# getBookTypeIndex(Convert.ToString(
              DataBinder.Eval(Container.DataItem,"Type"))) %>' 

        <td width="20%" valign="top">
          <asp:TextBox Id="txtBookPrice" Width="70"

              DataBinder.Eval(Container.DataItem,"price").ToString() %>'
        <td width="20%" valign="top">

          <asp:DropDownList ID="ddlBookPublisher" DataValueField="pub_id" 
            DataSource='<%# getBookPublisher() %>'
            SelectedIndex='<%# getBookPublisherIndex(Convert.ToString(
              DataBinder.Eval(Container.DataItem,"pub_id"))) %>'

        <td width="0">
          <asp:TextBox ID="txtBookID" Visible="false" Text='<%# 
            DataBinder.Eval(Container.DataItem,"title_id").ToString() %>' 

      <tr><td height="1" colspan="4" bgcolor="#E17546"></td></tr>

        <td colspan="4" align="center">
          <asp:Button ID="btnSubmit" OnClick="btnSubmit_Click"

            Text="Update Record" runat="server">


This web form uses a Repeater control to display book information. We introduce four web controls for data interaction. ddlBookType DropDownList - displays book type, txtBookPrice TextBox - displays book price, ddlBookPublisher DropDownList - displays book publisher, and txtBookID TextBox - hidden book ID.

We need to pay attention to two DropDownLists: Table Titles stores book type directly. But, for the sake of update, we need unique types for displaying. Thus, we call a function getBookType() to get DataSource. Then, using another function getBookTypeIndex() to set selected index for this book. This function passes individual book type, and returns selected index. Since table Titles only stores publishers ID, we need to get publisher name from table Publishers, and set a selected index for the individual book. getBookPublisher() and getBookPublisherIndex() functions do this job.

Finally, we put a button control in the FooterTemplate to fire a button click event.

The page displayed on web looks like following

Figure 1

When you click button "Update Record", the following code gets each book record's data, and updates it.

DropDownList typeSelected = new DropDownList();
DropDownList publisherSelected = new DropDownList();
TextBox priceText = new TextBox();
TextBox bookIdText = new TextBox(); 
string typeBook = null;
string publisherBook = null;
string priceBook = null;
string idBook = null;
double priceOfBook = 0.00;
foreach(RepeaterItem dataItem in repeaterBooks.Items)
   typeSelected = (DropDownList)dataItem.FindControl("ddlBookType");
   publisherSelected = (DropDownList)dataItem.FindControl("ddlBookPublisher");
   priceText = (TextBox)dataItem.FindControl("txtBookPrice");
   bookIdText = (TextBox)dataItem.FindControl("txtBookId");

   typeBook = typeSelected.SelectedItem.Value.ToString().Trim(); 
   publisherBook = publisherSelected.SelectedItem.Value.ToString().Trim();
   priceBook = priceText.Text.ToString().Trim();
   idBook =  bookIdText.Text.ToString().Trim();

   if (priceBook.Length == 0)
      priceOfBook = 0.00;
      priceOfBook = Convert.ToDouble(priceBook);

   pubDB updateBookRecord = new pubDB();


Here, we use foreach loop to go through Repeater control items, get each book record data with its book ID, then update it.

I use application logic layer to process all data. Presentation page only creates instances. The application logic layer includes all methods to process data. I attach this file at the end of this article.

If you want to use DataList or DataGrid control instead, you need to alter two parts of code.

  1. In web form, use DataList or DataGrid replace Repeater. For DataList, it is really simple, just replace Repeater with DataList if you don't want to change its ID. For DataGrid, you need to do a little bit more.
  2. In btnSubmit_Click event, change one sentence foreach(RepeaterItem dataItem in repeaterBooks.Items) to foreach(DataListItem dataItem in repeaterBooks.Items) for DataList or to foreach(DataGridItem dataItem in repeaterBooks.Items) for DataGrid.

How to make this sample work? You need to follow these steps:

  1. Create a folder called something like multipleUpdate somewhere on your server, create an application on IIS upon this folder;
  2. Create a subfolder called bin;
  3. Copy web.config, pubData.cs, and updaterecord.aspx to folder multipleUpdate;
  4. Switch your command directory to multipleUpdate folder, run this command line:
    csc /t:library /out:bin\multipleUpdate.dll /r:System.dll /r:System.Web.dll /r:System.Data.dll /r:System.Xml.dll pubData.cs

That's all! Now, I put all files here.


You can download a zip file containing all the files related to this article below.

About the Author

From ASP101

Articles originally posted on ASP101.com



  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds