469,140 Members | 1,158 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

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" %>
<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

Aug 3 '06 #1
1 1469
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
"Arpan" wrote:
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

Aug 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sue | last post: by
reply views Thread by Chumley the Walrus | last post: by
4 posts views Thread by Joe Van Meer | last post: by
reply views Thread by mkd1919 | last post: by
16 posts views Thread by William Buchanan | last post: by
3 posts views Thread by Augustin Prasanna | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.