ASP.NET Tip: Binding to Alternate Data Sources

Two of the controls introduced with .NET were the validation control and the validation summary control. I do a lot of server-side validation and I liked the concept of the validation summary, where all the errors on the form are shown at once. JavaScript nag windows annoy me when they validate one field at a time. The classes I generally use create a list of errors in a string format. However, this tip shows you how to create an ArrayList of an ErrorMessage class that can be bound to a Repeater, providing the same effect as the ValidationSummary.

Although .NET developers spend a lot of time talking about binding database-related objects to grids and repeaters, they don't talk much about binding their own objects to grids and lists. This can come in handy in many instances. This tip shows how to build a class that can be bound to a Repeater without involving any databases.

Let's start with a simple class that holds your error message:

public class ValidationError
   private string _error;

   public string ErrorMessage
      get { return _error; }
      set { _error = value; }

   public ValidationError(string errorMessage)
      ErrorMessage = errorMessage;

The Eval function used in conjunction with data binding will work only with properties, not public variables. That's why the property is defined. The class also has a constructor that takes the error message, which makes it easier to instantiate and populate the object in one statement.

Next, let's add a typical data entry form to enter information about a user. Any data entry form will work, and this form leaves off the extra HTML for clarity:

<form id="form1" runat="server">
   <asp:Repeater ID="rptErrors" runat="server" Visible="False">
         Please correct these errors:
         <li><%# Eval("ErrorMessage") %></li>
   <table cellspacing="0" cellpadding="3" border="0">
      <tr class="boldtext">
         <td>First Name:</td>
         <td><asp:TextBox ID="txtFirstName" CssClass="text"
                          runat="server" Width="200"
                          MaxLength="40" /></td>
      <tr class="boldtext">
         <td>Last Name:</td>
         <td><asp:TextBox ID="txtLastName" CssClass="text"
                          runat="server" Width="200"
                          MaxLength="40" /></td>
         <td colspan="2" align="center">
            <asp:LinkButton ID="btnSave"

The Repeater control at the top will build an unordered/bulleted list of your errors. A couple of entry boxes and a submit LinkButton round out the form. The following is the code behind:

public partial class SamplePage : System.Web.UI.Page
   protected override void OnInit(EventArgs e)
      btnSave.Click += new EventHandler(btnSave_Click);

   protected void Page_Load(object sender, EventArgs e)
      if (!Page.IsPostBack)
      rptErrors.Visible = false;


   void btnSave_Click(object sender, EventArgs e)
      ArrayList errors = new ArrayList();
      if (txtFirstName.Text == "")
         errors.Add(new ValidationError("First name is required."));

      if (txtLastName.Text == "")
         errors.Add(new ValidationError("Last name is required."));

      if (errors.Count > 0)
         rptErrors.DataSource = errors;
         rptErrors.Visible = true;
         // do save logic here and move to next page

You add an event handler for the LinkButton and initially hide the Repeater, since there won't be any errors when the form first loads. When the user hits the Save button, the form validates the two fields and adds the ValidationError objects to the ArrayList. When it's done validating, it checks for errors and if any are found, binds those to the Repeater. The Eval function in the Repeater (ASPX code) looks for the ErrorMessage property and displays the text in the list.

You can use this technique with any of your objects, regardless of function. The key is to create actual properties for any values you want to display.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at


  • 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

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date