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="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SQLClie nt" %>
<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;UI D=ud;PWD=pd")
sqlDapter = New SqlDataAdapter( "SELECT * FROM tblUsers",
sqlConn)
dSet = New DataSet()
sqlDapter.Fill( dSet, "Users")
dlUsers.DataSou rce = dSet.Tables("Us ers").DefaultVi ew
dlUsers.DataBin d()
End Sub
Sub ItemList(ByVal obj As Object, ByVal ea As
DataListCommand EventArgs)
dlUsers.Selecte dIndex = ea.Item.ItemInd ex
dlUsers.DataBin d()
End Sub
</script>
<body>
<form runat="server">
<asp:DataList ID="dlUsers" OnItemCommand=" ItemList" DataKeyField="I D"
runat="server">
<ItemTemplate >
<asp:LinkButt on ID="lnkFName" runat="server"> <%#
Container.DataI tem("FName") %></asp:LinkButton>
</ItemTemplate>
<SelectedItemTe mplate>
<%# Container.DataI tem("FName") & " " & Container.DataI tem("LName")
%><br>
Phone:<%# Container.DataI tem("Phone") %><br>
Address:<%# Container.DataI tem("Address") %><br>
</SelectedItemTem plate>
</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