zoukankan      html  css  js  c++  java
  • 手写dataview增删改查事件

    using System;
    using System.Data;
    using System.Configuration;
    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;
    using System.Data.SqlClient;

    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection sqlcon;
        SqlCommand sqlcmd;
        string strcon = "server=.;database=db_04;uid=sa;pwd=;";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.bind();
            }
        }
        public void bind()
        {
            string sqlstr = "select * from tb_Member";
            sqlcon = new SqlConnection(strcon);
            SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet ds = new DataSet();
            sqlcon.Open();
            myda.Fill(ds, "tb_Member");
            GridView1.DataSource = ds;
            GridView1.DataKeyNames = new string[] { "id" };
            GridView1.DataBind();
            sqlcon.Close();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlstr = "delete from tb_Member where id='"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";
            sqlcon = new SqlConnection(strcon);
            sqlcmd = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
            bind();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            sqlcon = new SqlConnection(strcon);
            string sqlstr = "update tb_Member set name='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',sex='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',nPlace='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
                + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            sqlcmd = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcmd.ExecuteNonQuery();
            sqlcon.Close();
            GridView1.EditIndex = -1;
            bind();
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }
    }

  • 相关阅读:
    hdu1050 Moving Tables
    初读《数学之美》........................(2)
    初读《数学之美》........................(1)
    zju1058 Currency Exchange
    hdu 2391 Filthy Rich
    hdu1029 Ignatius and the Princess IV(统计)
    hdu1072 Nightmare (BFS)
    apache2.2 + tomcat 6 集群
    权限管理系统实现:
    设置页面那些ID对应的对象隐藏setDisplay
  • 原文地址:https://www.cnblogs.com/wangheblog/p/2657180.html
Copyright © 2011-2022 走看看