日期:2014-05-17  浏览次数:20521 次

新人有点小问题~~~~~~求教~~~
从SQL里面查询出数据
添加到页面中的div层中
逐条显示。。。。
Thanks

------解决方案--------------------
repeater一次全部显示
datalistview一次显示一条记录
------解决方案--------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="display" runat="server">Table Will Go Here</div>
 
</form>
</body>
</html>

Default.aspx.cs后台代码
1: using System;
2: using System.Data;
3: using System.Data.SqlClient;
4: using System.Web.Configuration;
5:
6: public partial class Default2 : System.Web.UI.Page
7: {
8: protected void Page_Load(object sender, EventArgs e)
9: {
10: // Connection set up
11: String strConnection = WebConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;
12:
13: SqlConnection objConnection = new SqlConnection(strConnection);
14: String strSQL = "SELECT ProductName, UnitsInStock FROM Products";
15: // DataAdapter setup
16: SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConnection);
17: // DataSet & Adapter & Table
18: DataSet objDataSet = new DataSet();
19: objAdapter.Fill(objDataSet, "dtProducts");
20: String strResultsHolder;
21: strResultsHolder = "<table width=80% border=1>";
22: strResultsHolder += "<tr>";
23: foreach (DataColumn c in objDataSet.Tables["dtProducts"].Columns)
24: {
25: strResultsHolder += "<td>" + c.ColumnName + "</td>";
26: }
27: strResultsHolder += "</tr>";
28: int value, blankValue;
29: foreach (DataRow r in objDataSet.Tables["dtProducts"].Rows)
30: {
31: value = 100 * Convert.ToInt32(r["UnitsInStock"]) / 125;
32: blankValue = 100 - value;
33: strResultsHolder += "<tr><td width=30%>" + r["ProductName"] + "</td>" +
34: "<td width=60%><table width=100%><tr>" + "<td width=" + value.ToString() +
35: "% bgcolor=#9933FF>" + "&nbsp;</td>" + "<td width=" + blankValue.ToString() +
36: "%>&nbsp;</td>" + "</tr></table></td>" + "<td width=10%>" + r["UnitsInStock"].ToString() + "</td></tr>";
37: }
38: strResultsHolder += "</table>";
39: display.InnerHtml = strResultsHolder;
40: }
41: }