SQL_修改删除与GridView配合使用

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();

        }
阅读更多
个人分类: SQL .NET
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

SQL_修改删除与GridView配合使用

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭