8.8. Binding Data to a Web Forms FormView Control

Problem

You need to bind a result set to a FormView control and page through the data.

Solution

Follow these steps:

  1. The solution uses a table named FormView in the AdoDotNet35Cookbook database. Execute the following T-SQL statement to create the table:

    	USE AdoDotNet35Cookbook
    	GO
    	CREATE TABLE FormView(
    	    Id int NOT NULL PRIMARY KEY,
    	    IntField int NULL,
    	    StringField nvarchar(50) NULL )
  2. Execute the following T-SQL batch to create the sample data required by the solution:

    	USE AdoDotNet35Cookbook
    	GO
    	INSERT INTO FormView VALUES (1, 10, 'StringValue1')
    	INSERT INTO FormView VALUES (2, 20, 'StringValue2')
    	INSERT INTO FormView VALUES (3, 30, 'StringValue3')
    	INSERT INTO FormView VALUES (4, 40, 'StringValue4')
  3. Create a C# ASP.NET web application named BindWebFormFormView.

  4. Add the following controls to the Default.aspx design surface:

    • FormView named formView. Select Auto Format → Classic from the smart tag panel.

    The completed layout of the Web Form page Default.aspx is shown in Figure 8-15.

Layout for Default.aspx in BindWebFormFormView solution

Figure 8-15. Layout for Default.aspx in BindWebFormFormView solution

Example 8-15 shows the code in Default.aspx in the project BindWebFormFormView .

Example 8-15. File: Default.aspx for BindWebFormFormView solution

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BindWebFormFormView._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...

Get ADO.NET 3.5 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.