473,770 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Aug 3 '06 #1
1 1588
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 DataListCommand EventArgs 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="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

Aug 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1961
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" /> </headertemplate>
0
922
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 'yourname' when simple doing the following: <asp:DataList id="thedata" runat="server" CellSpacing="1"> <ItemTemplate> <%# Container.DataItem("yourname")%>
3
4957
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/ I want the datalist do display only the first row of the table. But it always fails. tried for exemple: dim nrowpos as ingeger 'shows the current rowposition dim dv as dataview
4
2482
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 where my last two columns I am displaying are ID fields, one for clientid and the other for empid. What I would really like is to display their actual names instead of the ids.
0
1172
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 page count of 20. This site executes successfully on my local machine and indexing service. When ran on the intended server, the app retrieves the record count, then binds the data in the 2nd DS, but doesn't display the records bound to the...
1
2330
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 my case the employees' details should be displayed in Alphabetical order.The employee names starting with "A" should display two extra HTML rows showing some more details.
0
1759
by: Jameel | last post by:
I just happened to visit ur post: http://groups.google.co.in/group/microsoft.public.dotnet.languages.vb/browse_thread/thread/ae471e2fdeeca01e/95061c1f95e30127?lnk=st&q=display+images+in+DataList+using+asp.net&rnum=35&hl=en#95061c1f95e30127
16
1768
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
2182
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 23949495 34945959
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.