惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - 可可NET

[导入].NET鼠标右键菜单JS[原] .NET鼠标右键菜单JS[原] - 可可NET - 博客园 [导入]关键字过滤方法 [导入]C#调用外部程序实现压缩和解压缩[原] [导入]用Table实现GridView控件功能 [导入]prototype 学习之简单实例 [导入]rep_daqjcrb [导入]asp.net程序中最常用的二十四种编程代码 [导入]原来查询语句还可以这样写[原] [导入]万能五笔输入法平台7.40版 8552KB 下载 [导入]C#面向对象设计模式纵横谈之7.Bridge 桥接模式 [导入]GreenBrowser - 你的绿色浏览器下载 [导入]柔道视频教程 2 [导入]柔道视频教程 1 [导入]搞笑手机动画图片 [导入]CodeRushNET(Eval)-2.1.3.exe [导入]原创摄影作品--朝阳[原] [导入]N年后,咱们这样坐飞机 [导入]强!看司机们如何整蛊那些公路收费站的[精]
用Table实现GridView控件功能
可可NET · 2009-04-24 · via 博客园 - 可可NET

        用Table实现GridView控件功能,很多时候大家都是用微软提供的控件,但有时候需要自己写的时候又不知道从哪里写起,这里讲的是用最简单的控件实现微软自带的强大控件功能,当然这里的需求并不是复杂的!

前台代码:

  1. <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="service.aspx.cs" Inherits="service" Title="Untitled Page" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  3.     <table style="width: 100%">
  4.         <tr>
  5.             <td style="text-align: right">
  6.                 員工姓名:<asp:TextBox ID="tb_name" runat="server"></asp:TextBox></td>
  7.             <td style="text-align: right">
  8.                 身份證號:<asp:TextBox ID="tb_idcard" runat="server"></asp:TextBox></td>
  9.             <td>
  10.                 <asp:Button ID="BT_Add" runat="server" OnClick="BT_Add_Click" Text="增加" /></td>
  11.         </tr>
  12.         <tr>
  13.             <td colspan="3">
  14.     <asp:Table ID="Table1" runat="server" style="text-align: left" BackColor="#C0C0FF" BorderColor="Black" BorderStyle="None" GridLines="Horizontal" Width="100%">
  15.     </asp:Table>
  16.             </td>
  17.         </tr>
  18.         <tr>
  19.             <td colspan="3" style="text-align: right;">
  20.                 <asp:Button ID="bt_one" runat="server" Text="第一页" OnClick="bt_one_Click" />
  21.                 <asp:Button ID="bt_up" runat="server" Text="上一页" OnClick="bt_up_Click" />
  22.                 <asp:Button ID="bt_next" runat="server" Text="下一页" OnClick="bt_next_Click" />
  23.                 <asp:Button ID="bt_last" runat="server" Text="最后页" />
  24.                     
  25.                 共 
  26.                 <asp:Label ID="lb_count" runat="server" Text="" style="font-weight: bold; color: #0000ff; text-decoration: none"></asp:Label>  页
  27.                 </td>
  28.         </tr>
  29.     </table>
  30.    <input id="h_pg" type="hidden" runat="server"  />
  31.     <input id="hf1" type="hidden" runat="server"  />
  32. </asp:Content>

后台代码:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using Test.Model;
  12. using Test.Bll;
  13. public partial class service : System.Web.UI.Page
  14. {
  15.     Test.Bll.Service bs = new Test.Bll.Service();
  16.     Test.Model.Service model = new Test.Model.Service();
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.         if (!IsPostBack)
  20.         {
  21.             hf1.Value = "0";
  22.             h_pg.Value = "0";
  23.         }
  24.         
  25.         DataTable dtc = bs.Getcount().Tables[0];
  26.         string pgnum = dtc.Rows[0][0].ToString();
  27.         
  28.         lb_count.Text = Convert.ToString(Convert.ToInt32(pgnum) / 10);
  29.         #region 下一页
  30.         if (Convert.ToInt32(h_pg.Value) < Convert.ToInt32(pgnum) / 10-1)
  31.         {
  32.             bt_next.Attributes.Add("onclick""document.getElementById('" + this.h_pg.ClientID + "').value=" + Convert.ToString(Convert.ToInt32(h_pg.Value) + 1) + ";");
  33.         }
  34.         else
  35.         {
  36.             bt_next.Attributes.Add("onclick""alert('已经是最后一页了!'); return;");
  37.         }
  38.         #endregion 
  39.         #region 上一页
  40.         if (Convert.ToInt32(h_pg.Value) <= Convert.ToInt32(pgnum) / 10 && Convert.ToInt32(h_pg.Value) != 0)
  41.         {
  42.             bt_up.Attributes.Add("onclick""document.getElementById('" + this.h_pg.ClientID + "').value=" + Convert.ToString(Convert.ToInt32(h_pg.Value) - 1) + ";");
  43.         }
  44.         else
  45.         {
  46.             bt_up.Attributes.Add("onclick""alert('已经是第一页了!'); return;");
  47.         }
  48.         #endregion 
  49.         #region 第一页
  50.         bt_one.Attributes.Add("onclick""document.getElementById('" + this.h_pg.ClientID + "').value=0;");
  51.         #endregion
  52.         #region 最后一页
  53.         bt_last.Attributes.Add("onclick""document.getElementById('" + this.h_pg.ClientID + "').value=" + Convert.ToString((Convert.ToInt32(pgnum) / 10-1)));
  54.         #endregion
  55.         #region 填充数据到Table1中,并实现编辑,保存,取消编辑,删除功能 
  56.         Table1.Controls.Clear();
  57.         string hf = hf1.Value;
  58.         int pg = Convert.ToInt32(h_pg.Value);
  59.         DataTable dt = bs.Getpagelist(pg).Tables[0];
  60.         TableRow tr;
  61.         TableCell td;
  62.         Label lb;
  63.         TextBox txt;
  64.         TableHeaderRow thr;
  65.         thr = new TableHeaderRow();
  66.         TableHeaderCell thd;
  67.         thd = new TableHeaderCell();
  68.         thd.Text = "員工編號";
  69.         thr.Cells.Add(thd);
  70.         thd = new TableHeaderCell();
  71.         thd.Text = "員工姓名";
  72.         thr.Cells.Add(thd);
  73.         thd = new TableHeaderCell();
  74.         thd.Text = "身份證號";
  75.         thr.Cells.Add(thd);
  76.         thd = new TableHeaderCell();
  77.         thd.Text = "操  作";
  78.         thr.Cells.Add(thd);
  79.         Table1.Controls.Add(thr);
  80.         for (int i = 0; i < dt.Rows.Count; i++)
  81.         {
  82.             tr = new TableRow();
  83.             td = new TableCell();
  84.             lb = new Label();
  85.             txt = new TextBox();
  86.             if (hf == "0")
  87.             {
  88.                 lb.Visible = true;
  89.                 txt.Visible = false;
  90.             }
  91.             else
  92.             {
  93.                 lb.Visible = false;
  94.                 txt.Visible = true;
  95.             }
  96.             lb.Text = dt.Rows[i][0].ToString();
  97.             td.Controls.Add(lb);
  98.             txt.Text = dt.Rows[i][0].ToString();
  99.             txt.ReadOnly = true;
  100.             txt.ID = "txt" + dt.Rows[i][0].ToString() + "_" + "0";
  101.             td.Controls.Add(txt);
  102.             tr.Cells.Add(td);
  103.             td = new TableCell();
  104.             lb = new Label();
  105.             txt = new TextBox();
  106.             if (hf == "0")
  107.             {
  108.                 lb.Visible = true;
  109.                 txt.Visible = false;
  110.             }
  111.             else
  112.             {
  113.                 lb.Visible = false;
  114.                 txt.Visible = true;
  115.             }
  116.             lb.Text = dt.Rows[i][1].ToString();
  117.             td.Controls.Add(lb);
  118.             txt.Text = dt.Rows[i][1].ToString();
  119.             txt.ID = "txt" + dt.Rows[i][0].ToString() + "_" + "1";
  120.             td.Controls.Add(txt);
  121.             tr.Cells.Add(td);
  122.             td = new TableCell();
  123.             lb = new Label();
  124.             txt = new TextBox();
  125.             if (hf == "0")
  126.             {
  127.                 lb.Visible = true;
  128.                 txt.Visible = false;
  129.             }
  130.             else
  131.             {
  132.                 lb.Visible = false;
  133.                 txt.Visible = true;
  134.             }
  135.             lb.Text = dt.Rows[i][2].ToString();
  136.             td.Controls.Add(lb);
  137.             txt.Text = dt.Rows[i][2].ToString();
  138.             txt.ID = "txt" + dt.Rows[i][0].ToString() + "_" + "2";
  139.             td.Controls.Add(txt);
  140.             tr.Cells.Add(td);
  141.             td = new TableCell();
  142.             Button bt;
  143.             Button btq;
  144.             Button bts;
  145.             bt = new Button();
  146.             bt.Text = "编辑";
  147.             bt.Attributes.Add("onclick""document.getElementById('" + this.hf1.ClientID + "').value=1;");
  148.             bt.Click += new EventHandler(bt_Click);
  149.             bts = new Button();
  150.             btq = new Button();
  151.             bts.Text = "保存";
  152.             btq.Text = "取消";
  153.             bts.Attributes.Add("onclick""document.getElementById('" + this.hf1.ClientID + "').value=0;");
  154.             btq.Attributes.Add("onclick""document.getElementById('" + this.hf1.ClientID + "').value=0;");
  155.             bts.ID = dt.Rows[i][0].ToString();
  156.             bts.Click += new EventHandler(bts_Click);
  157.             if (hf == "0")
  158.             {
  159.                 bt.Visible = true;
  160.                 bts.Visible = false;
  161.                 btq.Visible = false;
  162.             }
  163.             else
  164.             {
  165.                 bt.Visible = false;
  166.                 bts.Visible = true;
  167.                 btq.Visible = true;
  168.             }
  169.             td.Controls.Add(bt);
  170.             td.Controls.Add(bts);
  171.             td.Controls.Add(btq);
  172.             tr.Cells.Add(td);
  173.             Button btd = new Button();
  174.             btd.Text = "删除";
  175.             btd.ID = "btd" + dt.Rows[i][0].ToString();
  176.             btd.Click += new EventHandler(btd_Click);
  177.             td.Controls.Add(btd);
  178.             if (hf == "0")
  179.             {
  180.                 btd.Visible = true;
  181.             }
  182.             else 
  183.             {
  184.                 btd.Visible = false;
  185.             }
  186.             Table1.Controls.Add(tr);
  187.         }
  188.         #endregion
  189.     }
  190.     
  191.     
  192.     
  193.     
  194.     
  195.     void btd_Click(object sender, EventArgs e)
  196.     {
  197.         string id = ((Button)sender).ID;
  198.         string sql = id.Replace("btd""");
  199.         model.serviceid = Convert.ToInt32(sql);
  200.         int q = bs.Delete(model);
  201.         if (q > 0)
  202.         {
  203.             Response.Write("<script>alert('删除成功!');</script>");
  204.         }
  205.         else
  206.         {
  207.             Response.Write("<script>alert('删除失败!');</script>");
  208.         }
  209.         Page_Load(sender, e);
  210.     }
  211.     void bt_Click(object sender, EventArgs e)
  212.     {
  213.         Page_Load(sender, e);
  214.     }
  215.     
  216.     
  217.     
  218.     
  219.     
  220.     void bts_Click(object sender, EventArgs e)
  221.     {
  222.         string id = ((Button)sender).ID;
  223.         string sql = id.Replace("bts""");
  224.         model.serviceid = Convert.ToInt32(sql);
  225.         TextBox tb1 = ((TextBox)Table1.FindControl("txt" + sql + "_1"));
  226.         TextBox tb2 = ((TextBox)Table1.FindControl("txt" + sql + "_2"));
  227.         model.name = tb1.Text;
  228.         model.idcard =tb2.Text; 
  229.         int q = bs.Update(model);
  230.         if (q > 0)
  231.         {
  232.             Response.Write("<script>alert('修改成功!');</script>");
  233.         }
  234.         else
  235.         {
  236.             Response.Write("<script>alert('修改失败!');</script>");
  237.         }
  238.         Page_Load(sender, e);
  239.     }
  240.     #region 添加数据
  241.     
  242.     
  243.     
  244.     
  245.     
  246.     protected void BT_Add_Click(object sender, EventArgs e)
  247.     {
  248.         model.name = tb_name.Text;
  249.         model.idcard = tb_idcard.Text;
  250.         int q = bs.Add(model);
  251.         if (q > 0)
  252.         {
  253.             Response.Write("<script>alert('添加成功!');</script>");
  254.             tb_name.Text = "";
  255.             tb_idcard.Text = "";
  256.         }
  257.         else
  258.         {
  259.             Response.Write("<script>alert('添加失败!');</script>");
  260.         }
  261.         Page_Load(sender, e);
  262.     }
  263.     #endregion
  264.     
  265.     
  266.     
  267.     
  268.     
  269.     protected void bt_next_Click(object sender, EventArgs e)
  270.     {
  271.     }
  272.     
  273.     
  274.     
  275.     
  276.     
  277.     protected void bt_up_Click(object sender, EventArgs e)
  278.     {
  279.     }
  280.     
  281.     
  282.     
  283.     
  284.     
  285.     protected void bt_one_Click(object sender, EventArgs e)
  286.     {
  287.     }
  288. }

效果图