8.5. Binding Data to a Web Forms DataList Control
Problem
You need to bind the result set from a query to a DataList control and update the data source with changes, deletions, and new records using the DataList.
Solution
Follow the techniques demonstrated by this solution:
The solution uses a table named
DataListin theAdoDotNet35Cookbookdatabase. Execute the following T-SQL statement to create the table:USE AdoDotNet35Cookbook GO CREATE TABLE DataList( Id int NOT NULL PRIMARY KEY, IntField int NULL, StringField nvarchar(50) NULL )
The schema of table
DataListthat is used in the solution is shown in Table 8-4.Table 8-4. DataList table schema
Column name
Data type
Length
Allow nulls?
Idint4
No
IntFieldint4
Yes
StringFieldnvarchar50
Yes
Execute the following T-SQL batch to create the sample data needed for the solution.
USE AdoDotNet35Cookbook GO INSERT INTO DataList VALUES (1, 10, 'StringValue1') INSERT INTO DataList VALUES (2, 20, 'StringValue2') INSERT INTO DataList VALUES (3, 30, 'StringValue3') INSERT INTO DataList VALUES (4, 40, 'StringValue4')
Create a C# ASP.NET web application named
BindWebFormDataList.Add the following controls to the Default.aspx design surface:
DataListnameddataListButtonnamedinsertButtonwithTextproperty =Insert
The competed layout of the Web Form page Default.aspx is shown in Figure 8-9.

Figure 8-9. Layout for Default.aspx in BindWebFormDataList solution
The code ...
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