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:
The solution uses a table named
FormViewin theAdoDotNet35Cookbookdatabase. 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 )
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')
Create a C# ASP.NET web application named
BindWebFormFormView.Add the following controls to the Default.aspx design surface:
FormViewnamedformView. 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.

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access