关闭

SQL_修改删除与GridView配合使用

103人阅读 评论(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();

        }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2109次
    • 积分:82
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档