Listing 144 The Web Form chl45aspx Uses an itemTemplate to Display Student Data from the Class Records Database chl45aspxcs

private void Page Load(object sender, System.EventArgs e) { SqlConnection conn = new


("ClassRecordsSql")); conn.Open();

SqlCommand cm = new SqlCommand("Select StudentID, Lastname + ', '" +

"Firstname AS Name From Students ORDER BY Lastname ASC, " + "Firstname ASC", conn); SqlDataReader dr = null; DataSet ds = new DataSet(); System.Web.UI.ITemplate template = null; dr = cm.ExecuteReader();

template = Page.LoadTemplate("ch14-5ItemTemplate.ascx");

DataListl.ItemTemplate = template;

DataListl.DataSource = dr;



Figure 14.9 shows how the DataList renders when you browse to the ch14-5.aspx Web Form.

You would follow a similar procedure to add an AlternatingItemTemplate, a Header or Footer template, or any other template.

Figure 14.9: DataList with an ItemTemplate applied

Was this article helpful?

0 0

Post a comment