Display DB Records in DataList?

A

Arpan

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" %>
<html>
<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
SqlConnection("Server=(local);Database=DB1;UID=ud;PWD=pd")
sqlDapter = New SqlDataAdapter("SELECT * FROM tblUsers",
sqlConn)

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

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

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

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?

Thanks,

Arpan
 
G

Guest

Dear,
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
PostBack.

Hope this clarify your enquiry.

Reagrds,
--
Muhammad Mosa
Software Engineer & Solution Developer
MCT/MCSD.NET
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 In ItemDataBound Event 1
DataBind 1
IsPostBack 4
DropDownList DataGrid 1
DataGrid Edit Problem 9
ViewState? 1
Show "No Records Found" Message 1
DataGrid.Columns(Index) 3

Top