DataList Control
(Page 1 of 4 )
You've traveled the long and arduous journey of reading through my 3 other articles on using the DataGrid and Repeater controls in ASP.Net. This is the last but not least, the DataList. You can look at it as either a stripped-down DataGrid or a beefed-up repeater, but either way it's got a good mix of flexibility and functionality that will make it the best choice in many situations.
Previous articles on this topic include DataGrid and Repeater controls.
Introduction In my last article, I detailed how to make good use of the Repeater control. By setting up a template for the repeating data, you saw how easy it was to surface data from a dataset onto a web page. Beyond just merely repeating or displaying the data, I illustrated a real-life application of its use, putting it to work in an e-commerce environment. To do this I explain what is meant by 'event bubbling' and how to use it effectively.
You'll find it an easy transition to now move into the DataList control. Its use is very similar to the repeater, but it has some additional flexibility and functionality. You have more control over the display of the data, as well as provisions for data selection and editing.
Let's get right into the code, and I'll explain how it works along the way
Into the Code
Like any data-bound control in .Net, we'll start off the example by retreiving the actual data with which to populate the DataList. We don't really need to re-invent the wheel here; we can use very similar code to what is used with a repeater. We'll call the binding subroutine doBinding():
Sub doBinding()
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Ole DB Services=-4; " & _
"Data Source=C:\Inetpub\wwwroot\test.mdb"
Dim dbConnection As OleDbConnection = New OleDbConnection(strConn)
Dim strSQL As String = "SELECT * FROM products"
Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter( strSQL, strConn)
Dim dataSet As DataSet = New DataSet
dataAdapter.Fill(dataSet)
dList.DataSource = dataSet
dList.DataBind()
End Sub
Nothing too groundbreaking here. We just made the connection, established the query string with results in a dataset filled with the products data. Then we instantiate the datalist and bind the data to it.
Now of course this will all do exactly nothing unless we call the subroutine at some event. And naturally we will use the page load event to call it:
sub page_load( source as Object, e as EventArgs )
if not isPostBack then
doBinding()
end if
end sub
Next: Bring on the DataList >>
More ASP.NET Articles
More By Justin Cook