关闭

.net之后台代码填充前台数据

160人阅读 评论(0) 收藏 举报
分类:

在.net开发中,往往需要处理从数据库中读取的数据,在很多情况下,使用SQL语句进行操作就显得捉襟见肘,所以往往需要

通过后台代码对取出的数据进行操作,并填充到前台控件当中,下面这个例程就是通过后台代码对数据库中的成绩数据进行

汇总,填充到前台的表格中,而这些操作的关键,就是连接前台和后台的控件以及填充控件的ID属性。

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
    <style type="text/css">
    #Table_summary
    {
        border:1px solid black;
        }
    #Table_summary td , #Table_summary th
    {
        padding:10px;
        border:1px solid silver;
    }
    td span
    {
        margin:10px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="Table_summary" runat="server">
        <asp:TableHeaderRow>
        <asp:TableHeaderCell ColumnSpan="4">成绩汇总</asp:TableHeaderCell>
        </asp:TableHeaderRow>
        </asp:Table>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    string str_cnn = "Provider=MicroSoft.Jet.OLEDB.4.0;Data Source=";
    string str_sourcefile = "mydb.mdb";
    OleDbCommand cmd;
    OleDbConnection cnn;
    OleDbDataReader deter;
    string str_sql;

    protected void Page_Load(object sender, EventArgs e)
    {
        string[] _sarr1 = { "GOOD", "FAIL" };
        string[] _sarr2 = { "优秀", "不及格" };
        string[] _sarr_km = { "cj_yuwen", "cj_shuxue", "cj_wuli", "cj_huaxue", "cj_yingyu" };
        string[] _sarr_kmname = { "语文", "数学", "物理", "化学", "英语" };

        TableRow _tr;
        TableCell _td;

        for (int i = 0; i < _sarr_km.Length; i++) {
            _tr = new TableRow();
            for (int j = 0; j < _sarr1.Length; j++)
            {
                _td = new TableCell();
                _td.Text = _sarr_kmname[i] + _sarr2[j];
                _tr.Controls.Add(_td);

                _td = new TableCell();
                _td.ID = _sarr_km[i] + _sarr1[j]; //构建ID
                _tr.Controls.Add(_td);
            }
            Table_summary.Controls.Add(_tr);
        }


        string str_conn = str_cnn + MapPath(str_sourcefile);
        cnn = new OleDbConnection(str_conn);

        cnn.Open();

        str_sql = "SELECT * FROM tb_chenji";
        cmd = new OleDbCommand(str_sql, cnn);
        deter = cmd.ExecuteReader();

        Label label_name;
        Single _f;

        while (deter.Read()) {
            foreach (string _skm in _sarr_km) {
                _f = Convert.ToSingle(deter[_skm].ToString()); //注意_skm的应用<pre name="code" class="csharp"><span style="white-space: pre;">		</span>if (_f < 60) {
label_name = new Label(); label_name.Text = deter["cj_name"].ToString(); _td = (TableCell)FindControl(_skm + "FAIL"); //注意此处_skm的应用 _td.Controls.Add(label_name); } if (_f >85) { label_name = new Label(); label_name.Text = deter["cj_name"].ToString(); _td = (TableCell)FindControl(_skm + "GOOD"); //FindControl 寻找目标单元,需显示转换 _td.Controls.Add(label_name); } } } cnn.Close(); }}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:31487次
    • 积分:743
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:9篇
    • 译文:0篇
    • 评论:3条
    最新评论