473,372 Members | 998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 1567
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sue | last post by:
Anyone have any ideas on why the code below will show up in a browser's sourcecode as an empty table, and is not visible? aspx: <headertemplate> <asp:Table ID="MyTable" runat="server" /> ...
0
by: Chumley the Walrus | last post by:
I'm trying to display just one record from an sql dbase with a datalist control. My sql connection, statement, command, datasource, etc are all tested and correct, but I do not see the display for...
3
by: olle | last post by:
Hi everyone. Beging a newbee to asp.net and used to work with traditional asp I have one problem working with a datalist. 1/ I make a dataset as as session variable like session("employees") 2/...
4
by: Joe Van Meer | last post by:
Hi, Is the datalist meant for one table or can I use it to display information from 2 different tables? Or should I be looking at a datagrid instead? I began my code and kinda ran into a snag...
0
by: mkd1919 | last post by:
I have a website that performs a search on an indexing service and returns the results. During the initial load, I get the recordcount through a DS, then bind a DataList using a second DS with...
1
by: Kas_aspnet | last post by:
Hi, I have to display a sequence of records in an Aspx page. The problem is, i have to display the records by checking conditions like Classic ASP. For example, consider an employee table. In...
16
by: William Buchanan | last post by:
Hi folks I want to show 2 records on a page side by side. Each record has an image which will be displayed and a bit of text. How can I do this? Thanks Will
3
by: Augustin Prasanna | last post by:
I need to display some 200 records (dynamic) on the screen.. I need to display records from 2 tables (one to many relationship) output would look like Customer Id Name Phone NO 1 abc ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.