久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

ASP.NET中用后臺(tái)代碼創(chuàng)建DataTable和HTML表格

 悟靜 2011-07-05

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www./1999/xhtml" >
<head runat="server">
    <title>HTML表格測試</title>
    <style type="text/css">
        tr
        {
         line-height:22px;
        }
        A
        {
         color:#669966;        
        }
        .table_tr_Over
        {
            background-color:#F6F6F6;
        }
        .table_tr_Out
        {
            background-color:#FFFFFF;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>   
        <asp:PlaceHolder ID="plhTable" runat="server"></asp:PlaceHolder>   
    </div>
    </form>
</body>
</html>

后臺(tái)文件:

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

namespace WebHTML_Table
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            //獲取數(shù)據(jù)集
            DataTable tableSource = getTable();

            if (null == tableSource)
            {
                return;
            }

            //創(chuàng)建HTML表格
            HtmlTable tableHTML = new HtmlTable();
            //初始化表格
            tableHTML.Width = "100%";
            tableHTML.CellPadding = 0;
            tableHTML.CellSpacing = 0;
            tableHTML.Border = 1;

            //HTML行
            HtmlTableRow rowHTML = new HtmlTableRow();

            //HTML單元格
            HtmlTableCell cellHTML = new HtmlTableCell();

            //HTML<A>元素
            HtmlAnchor AnchorHTML;

            //第一步:將表格的標(biāo)題獲取出來
            for (int i = 0; i < tableSource.Columns.Count; i++)
            {
                cellHTML = new HtmlTableCell();
                cellHTML.Align = "center";
                cellHTML.VAlign = "middle";
                cellHTML.InnerHtml = "<b>" + tableSource.Columns[i].ToString() + "</b>";
                rowHTML.Cells.Add(cellHTML);
                tableHTML.Rows.Add(rowHTML);
            }

            //第二步:添加表格的內(nèi)容,,遍歷數(shù)據(jù)集
            foreach (DataRow item in tableSource.Rows)
            {
                //創(chuàng)建新的行
                rowHTML = new HtmlTableRow();

                //添加特效事件
                rowHTML.Attributes.Add("onmouseover", "this.className='table_tr_Over'");
                rowHTML.Attributes.Add("onmouseout", "this.className='table_tr_Out'");

                //依次循環(huán)tableSource中某一行的每一列,跟蹤一下就明白
                for (int i = 0; i < tableSource.Columns.Count; i++)
                {

                    //創(chuàng)建新的元素
                    AnchorHTML = new HtmlAnchor();
                    AnchorHTML.HRef = "Index.aspx";
                    //可根據(jù)item[i]或item["Name"]來獲取數(shù)據(jù)
                    AnchorHTML.InnerHtml = item[tableSource.Columns[i].ToString()].ToString();

                    //創(chuàng)建新的單元格
                    cellHTML = new HtmlTableCell();
                    //添加元素
                    cellHTML.Controls.Add(AnchorHTML);
                    //添加單元格
                    rowHTML.Cells.Add(cellHTML);
                }

                //向HTML表格中添加行
                tableHTML.Rows.Add(rowHTML);
            }


            //向PlaceHolder容器中添加表格
            plhTable.Controls.Add(tableHTML);
        }

        /// <summary>
        /// 獲取數(shù)據(jù)集
        /// </summary>
        /// <returns></returns>
        private DataTable getTable()
        {
            //創(chuàng)建表
            DataTable tableInfo = new DataTable();

            //列
            DataColumn dcName = new DataColumn("Name", Type.GetType("System.String"));
            DataColumn dcEmail = new DataColumn("Email", Type.GetType("System.String"));
            DataColumn dcAddress = new DataColumn("Address", Type.GetType("System.String"));

            //添加列
            tableInfo.Columns.Add(dcName);
            tableInfo.Columns.Add(dcEmail);
            tableInfo.Columns.Add(dcAddress);

            for (int i = 0; i < 10; i++)
            {
                //行
                DataRow dr = tableInfo.NewRow();
                dr["Name"] = "Name" + i.ToString();
                dr["Email"] = "Email" + i.ToString();
                dr["Address"] = "Address" + i.ToString();

                //添加行
                tableInfo.Rows.Add(dr);
            }

            return tableInfo;
        }

    }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多