DataSet绑定数据到HTML的Table

执行效果图

<%@ 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>

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

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:  }

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }原文链接:https://www.cnblogs.com/apiaceae/archive/2009/05/13/1456289.html
本文来源 爱码网,其版权均为 原网址 所有 与本站无关,文章内容系作者个人观点,不代表 本站 对观点赞同或支持。如需转载,请注明文章来源。

© 版权声明

相关文章