|
DataList is the feature-rich control included in the Asp.Net framework. They include the concept of Event Bubbling, Template that will manipulate the display of data and the Data Keys collection.
Event Bubbling:
The event bubbling concept provided by DataList, DataGrid and Repeater Control will help you to capture the events in their child control. When an event is raised in a child control, the event “bubbles up” to the containing control, and the containing control can then execute a subroutine to handle the event.
Main events in DataList:
The following five are the main events in the Datalist.
- ItemCommand: Raised by a child control when an event is not handled by CancelCommand, Delete Command, EditCommand or Update Command.
- UpdateCommand: capture event raised by child control with CommandName=”update”
- EditCommand: capture event raised by child control with CommandName=”edit”
- DeleteCommand: capture event raised by child control with CommandName=”delete”
- CancelCommand: capture event raised by child control with CommandName=”cancel”
Templates that are used to format the output of the control.
We can format the output in the ASP.net by using the pre defined templates. The following seven templates will be used to format the output.
- HeaderTemplate: format the header for DataList
- ItemTemplate: format each item in the DataList
- AlternatingItemTemplate: control the formatting for alternate item.
- SeparatorTemplate: Display a separator b/w each item displayed.
- FooterTemplate: format footer.
- SelectedItemTemplate: control how selected item is formatted.
- EditItemTemplate: control how an item selected for editing is formatted.
RepeatColumns and RepeatDirection
The column layout of the datalist can be controlled using the RepeatColumns and RepeatDirection property. The RepeatColumns property determines the number of columns to be displayed whereas the RepeatDirection determines whether columns should be repeated horizontally or vertically. By default its value is vertical.
Example:-
Note: -This example will fetch the records from database and displayed the records in datalist control. In the example we display the first name and last name of the Employee.
DataListContro.aspx
<% @ Page Language ="VB" AutoEventWireup ="false" CodeFile ="Example12.aspx.vb" Inherits ="Example12" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server">
</ head >
< body >
< form id ="form1" runat ="server">
< div >
< asp : DataList ID ="DataList1" runat ="server" RepeatColumns ="2" RepeatDirection ="Horizontal" Width ="192px">
< ItemTemplate >
<% # Container.DataItem( "fname" ) %>
< br >
<% # Container.DataItem( "lname" ) %>
</ ItemTemplate >
< SelectedItemTemplate >< b >< i >
<% # Container.DataItem( "fname" ) %>
< br >
<% # Container.DataItem( "lname" ) %> </ i ></ b >
</ SelectedItemTemplate >
</ asp : DataList ></ div >
</ form >
</ body >
</ html >
DataListControl.aspx.vb
Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class Example12 Inherits System.Web.UI.Page
Protected Sub Page_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
Dim con As SqlConnection
Dim ada As SqlDataAdapter
Dim dst As DataSet
con = New SqlConnection( "Data Source=localhost;Initial catalog=pubs;UID=sa;pwd=sa" )
ada = New SqlDataAdapter( "select * from Employee" , con)
dst = New DataSet
con.Open()
ada.Fill(dst, "Employee" )
con.Close()
DataList1.DataSource = dst.Tables( "Employee" )
DataList1.DataBind()
End Sub
End Class |