Problem: Popolating HTML Table Dynamically
-
Hi There.
I am having a problem in populating html table from database dynamically. What i mean is that ihave a table in database that have let say 10 enteries, I want to display those 10 entries in a
html table using loop. I am new to ASP.NET, I've previously woked in php. I can present the
scenario in php and please help me do the same thing in ASP.NET. In php what i normally do is
search the database, grab the table in fetch array and in html table i create one row and use
while loop so that all of the 10 entries are populated at runtime. Here is a demonstration
code in head:
<?php
$query = mysql_query("select * from table");
?>in body code:
<table>
<tr>
<td>Username</td>
<td>Email</td>
<td>Position</td>
<td>Status</td>
</td>
//start of the loop
<?php while($fetch = mysql_fetch_array($query) { ?>
<tr>
<td><?php echo $fetch["username"]; ?> </td>
<td><?php echo $fetch["email"]; ?> </td>
<td><?php echo $fetch["position"]; ?> </td>
<td><?php echo $fetch["status"]; ?> </td>
</tr><?php } ?>//End of loop
</table>When i use foreach loop in asp.net to do the same thing the it displays only the last row in the
database table...I am using three tier architecture...
projectsLoad_BLL bo = new projectsLoad_BLL();
DataSet ds = new DataSet(); DataTable dt = ds.Tables\[0\]; int num = 1; foreach (DataRow dr in dt.Rows) { lblno.Text = num.ToString(); lblusername.Text = dr\["username"\].ToString(); lblemail.Text = dr\["email"\].ToString(); lblposition.Text = dr\["position"\].ToString(); lblstatus.Text = dr\["status"\].ToString(); dt.NewRow(); num++; }
In HTML Table:
-
Hi There.
I am having a problem in populating html table from database dynamically. What i mean is that ihave a table in database that have let say 10 enteries, I want to display those 10 entries in a
html table using loop. I am new to ASP.NET, I've previously woked in php. I can present the
scenario in php and please help me do the same thing in ASP.NET. In php what i normally do is
search the database, grab the table in fetch array and in html table i create one row and use
while loop so that all of the 10 entries are populated at runtime. Here is a demonstration
code in head:
<?php
$query = mysql_query("select * from table");
?>in body code:
<table>
<tr>
<td>Username</td>
<td>Email</td>
<td>Position</td>
<td>Status</td>
</td>
//start of the loop
<?php while($fetch = mysql_fetch_array($query) { ?>
<tr>
<td><?php echo $fetch["username"]; ?> </td>
<td><?php echo $fetch["email"]; ?> </td>
<td><?php echo $fetch["position"]; ?> </td>
<td><?php echo $fetch["status"]; ?> </td>
</tr><?php } ?>//End of loop
</table>When i use foreach loop in asp.net to do the same thing the it displays only the last row in the
database table...I am using three tier architecture...
projectsLoad_BLL bo = new projectsLoad_BLL();
DataSet ds = new DataSet(); DataTable dt = ds.Tables\[0\]; int num = 1; foreach (DataRow dr in dt.Rows) { lblno.Text = num.ToString(); lblusername.Text = dr\["username"\].ToString(); lblemail.Text = dr\["email"\].ToString(); lblposition.Text = dr\["position"\].ToString(); lblstatus.Text = dr\["status"\].ToString(); dt.NewRow(); num++; }
In HTML Table:
-
Hi There.
I am having a problem in populating html table from database dynamically. What i mean is that ihave a table in database that have let say 10 enteries, I want to display those 10 entries in a
html table using loop. I am new to ASP.NET, I've previously woked in php. I can present the
scenario in php and please help me do the same thing in ASP.NET. In php what i normally do is
search the database, grab the table in fetch array and in html table i create one row and use
while loop so that all of the 10 entries are populated at runtime. Here is a demonstration
code in head:
<?php
$query = mysql_query("select * from table");
?>in body code:
<table>
<tr>
<td>Username</td>
<td>Email</td>
<td>Position</td>
<td>Status</td>
</td>
//start of the loop
<?php while($fetch = mysql_fetch_array($query) { ?>
<tr>
<td><?php echo $fetch["username"]; ?> </td>
<td><?php echo $fetch["email"]; ?> </td>
<td><?php echo $fetch["position"]; ?> </td>
<td><?php echo $fetch["status"]; ?> </td>
</tr><?php } ?>//End of loop
</table>When i use foreach loop in asp.net to do the same thing the it displays only the last row in the
database table...I am using three tier architecture...
projectsLoad_BLL bo = new projectsLoad_BLL();
DataSet ds = new DataSet(); DataTable dt = ds.Tables\[0\]; int num = 1; foreach (DataRow dr in dt.Rows) { lblno.Text = num.ToString(); lblusername.Text = dr\["username"\].ToString(); lblemail.Text = dr\["email"\].ToString(); lblposition.Text = dr\["position"\].ToString(); lblstatus.Text = dr\["status"\].ToString(); dt.NewRow(); num++; }
In HTML Table:
You should create a container, it can be a table called table_Container, and then loop threw the dataset, and create your tableRow and cells in code. You can use the repeater control, but I think you should learn the basics first. Do not use Response.write(""), because response write is reserved for higher level responses like a page error code, or a data set or something. PHP doesn't have html objects, so echo("tr" is quite common, but is poor practice in general.
foreach (DataRow dr in dt.Rows) {
TableRow tr = new TableRow; table\_Container.controls.add(tr); TableCell td = new TableCell; tr.controls.add(td); Label lbl\_UserName = new Label; lbl\_UserName.text = dr\["username"\]; td.controls.add(lbl\_UserName);
}
-
You should create a container, it can be a table called table_Container, and then loop threw the dataset, and create your tableRow and cells in code. You can use the repeater control, but I think you should learn the basics first. Do not use Response.write(""), because response write is reserved for higher level responses like a page error code, or a data set or something. PHP doesn't have html objects, so echo("tr" is quite common, but is poor practice in general.
foreach (DataRow dr in dt.Rows) {
TableRow tr = new TableRow; table\_Container.controls.add(tr); TableCell td = new TableCell; tr.controls.add(td); Label lbl\_UserName = new Label; lbl\_UserName.text = dr\["username"\]; td.controls.add(lbl\_UserName);
}
I totally agree with him that you should learn basic thing how to create response.write(". ;)
-
Thank You very much for your help. I've managed to use asp repeater with datatables efficiently. What i did is added a repeater in asp markup and bind it to dataset. And in i've placed <%# Eval("columnName") %> and viola. All operations are working fine. Thanks again!
-
Thank You very much for your help. I've managed to use asp repeater with datatables efficiently. What i did is added a repeater in asp markup and bind it to dataset. And in i've placed <%# Eval("columnName") %> and viola. All operations are working fine. Thanks again!
Using
Repeater
is certainly a very good choice. I am happy that it worked for you :) I have one more suggestion - if you are having only read-only records i.e. you only view them, then consider usingSqlDataReader
object instead of theDataSet
as it is fast - read-only - forward only. Happy to help you :)Always Keep Smiling. Yours Pankaj Nikam