SQL_修改删除与GridView配合使用

3 篇文章 0 订阅

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Data.SqlClient;

using System.Text.RegularExpressions;

namespace WebApplication1

{

    public partial class userinfo : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                band();

            }

        }

        private void band()

        {

            SqlConnection con = new SqlConnection("Server=localhost;database=qcwx;uid=sa;pwd=sasasa");

            con.Open();

            SqlDataAdapter sda = new SqlDataAdapter("select * from Staff", con);

            DataSet ds = new DataSet();

            sda.Fill(ds);

            this.GridView1.DataSource = ds;

            this.GridView1.DataBind();

            con.Close();

            this.labpage.Text = "当前页为第" + (this.GridView1.PageIndex + 1).ToString() + "页 共有" + (this.GridView1.PageCount).ToString() + "页";

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

        {

            this.GridView1.EditIndex = -1;

            band();

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

        {

            this.GridView1.EditIndex = e.NewEditIndex;

            band();

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

        {

            SqlConnection con = new SqlConnection("Server=localhost;database=qcwx;uid=sa;pwd=sasasa");

            con.Open();

            string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();

            string s_name = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("tname")).Text.ToString();

            string s_sex = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("tsex")).Text.ToString();

            string s_birthday = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("tdate")).Text.ToString();

            string s_phone = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("tcell")).Text.ToString();

            string s_mobile = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("tphone")).Text.ToString();

            string s_email = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("temail")).Text.ToString();

            string cmdtext = "update Staff set s_name='" + s_name + "',s_sex='" + s_sex + "',s_birthday='" + s_birthday + "',s_phone='" + s_phone + "',s_mobile='" + s_mobile + "',s_email='" + s_email + "' where s_id='" + id + "'";

            SqlCommand cmd = new SqlCommand(cmdtext, con);

            cmd.ExecuteNonQuery();

            this.GridView1.EditIndex = -1;

            band();

            Response.Write("<script>alert('更新成功!!')</script>");

 

 

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

        {

            SqlConnection con = new SqlConnection("Server=localhost;database=qcwx;uid=sa;pwd=sasasa");

            con.Open();

            string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();

            string cmdtext = "delete from Staff where s_id='" + id + "'";

            SqlCommand cmd = new SqlCommand(cmdtext, con);

            cmd.ExecuteNonQuery();

            band();

            Response.Write("<script>alert('删除成功!!')</script>");

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

        {

            if (e.Row.RowType == DataControlRowType.DataRow)

            {

                //判断GridView控件当前的状态

                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)

                {

                    ((LinkButton)e.Row.FindControl("del")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除\"" + ((Label)e.Row.FindControl("s_id")).Text + "\"用户吗?')");//?

                }

            }

 

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

        {

            this.GridView1.PageIndex = e.NewPageIndex;

            band();

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

        {

            this.GridView1.PageSize = Convert.ToInt32(this.DropDownList1.SelectedValue);

            band();

        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值