后台写代码让table实现Gridview的编辑删除更新的功能

后台代码:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace FileUpLoadRegister
{
    public partial class ShowUserInfo : System.Web.UI.Page
    {
        Table t1 = null;
        TextBox txt =null;
        Label lbl = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=login;uid=sa;pwd=sa");
            SqlDataAdapter sda = new SqlDataAdapter("select * from userlogin", conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "userlogin");
            t1 = new Table();
            t1.BorderWidth = 1;
            t1.GridLines = GridLines.Both;
            t1.BorderColor = System.Drawing.Color.Black;
            t1.CellPadding = 0;
            t1.CellSpacing = 0;
            t1.Width = 600;
            TableRow tr1 = new TableRow();
            tr1.ForeColor = System.Drawing.Color.Green;
            tr1.BorderColor = System.Drawing.Color.Black;
            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            TableCell tc3 = new TableCell();
            TableCell tc4 = new TableCell();
            TableCell tc5 = new TableCell();
            tc1.Text = "ID";
            tc2.Text = "UserName";
            tc3.Text = "UserPswd";
            tc4.Text = "Update";
            tc5.Text = "Delete";
            tr1.Cells.Add(tc1);
            tr1.Cells.Add(tc2);
            tr1.Cells.Add(tc3);
            tr1.Cells.Add(tc4);
            tr1.Cells.Add(tc5);
            t1.Rows.Add(tr1);
            for (int i = 0; i < ds.Tables["userlogin"].Rows.Count; i++)//循环行
            {
                TableRow tr = new TableRow();
                for (int j = 0; j < ds.Tables["userlogin"].Columns.Count; j++)//循环列
                {
                    TableCell tc = new TableCell();//在列里面循环添加两个控件TextBox和Label 先将TextBox隐藏,将表的数据循环赋值给label
                    txt = new TextBox();
                    lbl = new Label();
                    tc.Controls.Add(lbl); ;
                    tc.Controls.Add(txt);
                    txt.Visible = false;
                    lbl.Text= ds.Tables["userlogin"].Rows[i][j].ToString();
                    tr.Cells.Add(tc);
                }
                Button btnUpdate = new Button();//循环在行里添加两列,再循环在两列里添加四个按钮,修改和删除可见,更新和取消隐藏
                btnUpdate.Text = "修改";
                btnUpdate.Visible = true;
                Button btnQuxiao = new Button();
                btnQuxiao.Visible = false;
                btnQuxiao.Text = "取消";
                Button btnNews = new Button();
                btnNews.Visible = false;
                btnNews.Text = "更新";
                Button btnDelete = new Button();
                btnDelete.Text = "删除";
                btnDelete.Visible = true;
                btnDelete.CommandName = ds.Tables[0].Rows[i][0].ToString();//将按钮和每行的第一个字段ID联系起来
                btnUpdate.CommandName = ds.Tables[0].Rows[i][0].ToString(); ;
                TableCell tcbtnd = new TableCell();
                TableCell tcbtnu = new TableCell();
                tcbtnd.Controls.Add(btnDelete);
                tcbtnd.Controls.Add(btnQuxiao);
                tcbtnu.Controls.Add(btnUpdate);
                tcbtnu.Controls.Add(btnNews);
                tr.Cells.Add(tcbtnu);
                tr.Cells.Add(tcbtnd);
                t1.Rows.Add(tr);//添加到表中
                btnDelete.Click += new EventHandler(btnDelete_Click);
                btnNews.Click+=new EventHandler(btnNews_Click);
                btnUpdate.Click += new EventHandler(btnUpdate_Click);
                btnQuxiao.Click += new EventHandler(btnQuxiao_Click);
                tr.BorderColor = System.Drawing.Color.Black;
            }
            Page.Form.Controls.Add(t1);
        }

        void btnQuxiao_Click(object sender, EventArgs e)//点击取消按钮时
        {
            Page.RegisterStartupScript("提示", "<script>window.history.go(-1)</script>");//取消返回上一个页面
        }

        void btnNews_Click(object sender, EventArgs e)
        {
            TableCell tca = (TableCell)(((Button)sender).Parent);//单元格内容的父级是列

            TableRow tra = (TableRow)tca.Parent;//列的父级是行

            ((Button)tra.Cells[3].Controls[1]).Visible = false;
            ((Button)tra.Cells[3].Controls[0]).Visible = true;
            ((Button)tra.Cells[4].Controls[1]).Visible = false;
            ((Button)tra.Cells[4].Controls[0]).Visible = true;
            for (int j = 1; j < tra.Cells.Count - 2; j++)//因为添加了两列所以要除去两列并且第一列的ID不能修改
            {
                ((TextBox)tra.Cells[j].Controls[1]).Visible = false;
                ((Label)tra.Cells[j].Controls[0]).Text = "";
            }
            SqlConnection conn = new SqlConnection("server=.;database=login;uid=sa;pwd=sa");
            conn.Open();
            SqlCommand cmd = new SqlCommand("update userlogin set username=@name,userpswd=@pswd where userId=@id", conn);
            SqlParameter id = new SqlParameter("@id",((Label)tra.Cells[0].Controls[0]).Text);
            SqlParameter name = new SqlParameter("@name",((TextBox)tra.Cells[1].Controls[1]).Text);
            SqlParameter pswd = new SqlParameter("@pswd",((TextBox)tra.Cells[2].Controls[1]).Text);
            cmd.Parameters.Add(id);
            cmd.Parameters.Add(name);
            cmd.Parameters.Add(pswd);
            cmd.ExecuteNonQuery();
            conn.Close();
            Page.RegisterStartupScript("提示", "<script>window.location='ShowUserInfo.aspx'</script>");//刷新
        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
            TableCell tca = (TableCell)(((Button)sender).Parent);
            TableRow tra = (TableRow)tca.Parent;
            ((Button)tra.Cells[3].Controls[0]).Visible = false;
            ((Button)tra.Cells[3].Controls[1]).Visible = true;
            ((Button)tra.Cells[4].Controls[0]).Visible = false;
            ((Button)tra.Cells[4].Controls[1]).Visible = true;
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select * from userlogin", "server=.;database=login;uid=sa;pwd=sa");
            sda.Fill(ds, "userlogin");
            for (int i = 1; i < tra.Cells.Count - 2; i++)
            {
                ((TextBox)tra.Cells[i].Controls[1]).Visible = true;
                ((TextBox)tra.Cells[i].Controls[1]).Text = ((Label)tra.Cells[i].Controls[0]).Text;
                ((Label)tra.Cells[i].Controls[0]).Text="";
            }
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=login;uid=sa;pwd=sa");
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete from login where userid=@id" ,conn);
            SqlParameter id = new SqlParameter("@id",((Button)sender).CommandName);
            cmd.Parameters.Add(id);
            cmd.ExecuteNonQuery();
            conn.Close();
          
        }
    }
}

转载于:https://www.cnblogs.com/bky250214511/archive/2008/11/08/1329692.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值