473,387 Members | 1,834 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,387 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 1568
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 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.