Display DB Records in DataList?



The following code displays records from a SQL Server 7 database table
in a DataList (when the page loads for the first time, all the records
under the column named "FName" in the DB table are displayed as links
which, when clicked, displays the records existing in the other columns
corresponding to the link clicked):

<%@ Page Language=VB Explicit="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New
sqlDapter = New SqlDataAdapter("SELECT * FROM tblUsers",

dSet = New DataSet()
sqlDapter.Fill(dSet, "Users")

dlUsers.DataSource = dSet.Tables("Users").DefaultView
End Sub

Sub ItemList(ByVal obj As Object, ByVal ea As
dlUsers.SelectedIndex = ea.Item.ItemIndex
End Sub
<form runat="server">
<asp:DataList ID="dlUsers" OnItemCommand="ItemList" DataKeyField="ID"
<asp:LinkButton ID="lnkFName" runat="server"><%#
Container.DataItem("FName") %></asp:LinkButton>
<%# Container.DataItem("FName") & " " & Container.DataItem("LName")
Phone:<%# Container.DataItem("Phone") %><br>
Address:<%# Container.DataItem("Address") %><br>

As such the above code works fine but what I fail to understand is how
does the sub named "ListItem" know which records have to be retrieved
from the different columns when a link is clicked? Is it because the
DataSet with all the records have already been loaded in the memory?

In other words, the "ListItem" sub executes the following SQL query:

SELECT * FROM tblUsers WHERE FName='" & <whichever link has been
clicked> & "'"

But the "ListItem" sub doesn't have any SQL query like above; so how is
it filtering the records based on the "FName" column & then displaying
them in the DataList?






1)fist this you need to notice is: the code in you page load is not enclosed
with check if IsPostBack is set to false so whenever the page is loaded, your
data set is going to be loaded too.

2)When you click on your LinkButton, your page is going to PostBack, and you
page load event handler method will be called, and your DataSet according to
what I had mentioned above is going to be loaded.

3)Now and after Page load is exeuted, your item command event handler will
exeute, and your page sent some information to that handler through page
PostBack and it is enclosed in the DataListCommandEventArgs Like information
about the item u clicked as its index on the datalist.

4)Before you click on your LinkButton. move your mouse over it and check out
your IE status bar, you'll find a javascript method like this __doPostBack
this is how information about your clicked item is going to be sent in the

Hope this clarify your enquiry.

Muhammad Mosa
Software Engineer & Solution Developer
MCTS: .Net 2.0 Web Applications
MCTS: .Net 2.0 Windows Applications

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DataBind 1
DataBind In ItemDataBound Event 1
IsPostBack 4
DropDownList DataGrid 1
ViewState? 1
DataGrid Edit Problem 9
DataGrid.Columns(Index) 3
Insert New Record 3