我用gridview绑定数据,自己写的代码。数据有分页有删除功能,在绑定的代码上面+if(!ispostback)分页就失效,要是不加,每次删除一条记录,刷新的时候就会重复删除一条,怎么解决啊?

解决方案 »

  1.   

    using System;
    using System.Data;
    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;
    using System.Data.SqlClient;public partial class HR_HRAdmin_AppCheck : System.Web.UI.Page
    {
          
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        public void bind()
        {
            string cmdtxt1 = "server=(local);database=HrSQL;Uid=sa;Pwd=fov152587018";
            SqlConnection Con = new SqlConnection(cmdtxt1);
            Con.Open();
            string sql = "select   *   from   [CHECKWORK]   where  1=1   ";
            string sqlend = "order   by   id   desc";
            string sqlwhere = string.Empty;
            if (this.TextBoxname.Text.Trim() != "")
            {
                sqlwhere += "and  C_ename like  '%" + this.TextBoxname.Text.Trim() + "%'  ";
            }
            if (this.TextBoxtimebegin.Text.Trim() != "" && this.TextBoxtimeend.Text.Trim() != "")
            {
                sqlwhere += "and C_start between  '" + this.TextBoxtimebegin.Text.Trim() + "'and '" + this.TextBoxtimeend.Text.Trim() + "'";
            }
            if (this.DropDownList1.SelectedValue.ToString() != "")
            {
                sqlwhere += "and C_branch =  '" + this.DropDownList1.SelectedValue.ToString() + "'  ";
            }
            sql += sqlwhere + sqlend;
            SqlDataAdapter Da = new SqlDataAdapter(sql, Con);
            DataSet ds = new DataSet();
            Da.Fill(ds);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            bind();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;database=HrSQL;uid=sa;pwd=fov152587018");
            con.Open();
            SqlCommand com = new SqlCommand("delete from [CHECKWORK] where id='" + GridView1.DataKeys[e.RowIndex].Value + "'", con);
            com.ExecuteNonQuery();
            SqlDataAdapter ada = new SqlDataAdapter("select * from [CHECKWORK] order by id desc", con);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            con.Close();
        }
    }
      

  2.   

    附代码C#using System;
    using System.Data;
    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;
    using System.Data.SqlClient;public partial class HR_HRAdmin_AppCheck : System.Web.UI.Page
    {
          
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        public void bind()
        {
            string cmdtxt1 = "server=(local);database=HrSQL;Uid=sa;Pwd=fov152587018";
            SqlConnection Con = new SqlConnection(cmdtxt1);
            Con.Open();
            string sql = "select   *   from   [CHECKWORK]   where  1=1   ";
            string sqlend = "order   by   id   desc";
            string sqlwhere = string.Empty;
            if (this.TextBoxname.Text.Trim() != "")
            {
                sqlwhere += "and  C_ename like  '%" + this.TextBoxname.Text.Trim() + "%'  ";
            }
            if (this.TextBoxtimebegin.Text.Trim() != "" && this.TextBoxtimeend.Text.Trim() != "")
            {
                sqlwhere += "and C_start between  '" + this.TextBoxtimebegin.Text.Trim() + "'and '" + this.TextBoxtimeend.Text.Trim() + "'";
            }
            if (this.DropDownList1.SelectedValue.ToString() != "")
            {
                sqlwhere += "and C_branch =  '" + this.DropDownList1.SelectedValue.ToString() + "'  ";
            }
            sql += sqlwhere + sqlend;
            SqlDataAdapter Da = new SqlDataAdapter(sql, Con);
            DataSet ds = new DataSet();
            Da.Fill(ds);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            bind();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;database=HrSQL;uid=sa;pwd=fov152587018");
            con.Open();
            SqlCommand com = new SqlCommand("delete from [CHECKWORK] where id='" + GridView1.DataKeys[e.RowIndex].Value + "'", con);
            com.ExecuteNonQuery();
            SqlDataAdapter ada = new SqlDataAdapter("select * from [CHECKWORK] order by id desc", con);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            con.Close();
        }
    }
      

  3.   

    using System;
    using System.Data;
    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;
    using System.Data.SqlClient;
    using BJBussiness; 
     
    public partial class Manage_CardInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindList();
            }
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int CID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            Card CardInfo = new Card();
            CardInfo.CID = CID;
            if (Card.DeleteCard(CardInfo))
            {
                //Jscript.Alert("删除成功");
                BindList();
            }
        }
        /// <summary>
        /// 显示所有的信息记录数
        /// </summary>
        public void BindList()
        {
            DataTable dt = Card.GetCardInfo();
            this.AspNetPager1.RecordCount = dt.Rows.Count;
            BindListInfo();
        }
        /// <summary>
        /// 显示信息列表
        /// </summary>
        public void BindListInfo()
        {
            SqlConnection getConn = SQlData.ReturnConn();
            string procname = "select * from MemberCard";
            SqlDataAdapter da = new SqlDataAdapter(procname, getConn);
            DataSet ds = new DataSet();
            da.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "SYSS");
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells[3].HasControls())
            {
                LinkButton BtnDelete = (LinkButton)e.Row.Cells[3].Controls[0];
                //LinkButton BtnUpdate = (LinkButton)e.Row.Cells[4].Controls[0];
                if (BtnDelete.Text.Equals("删除"))
                {
                    BtnDelete.Attributes.Add("onclick", "javascript:return confirm('" + string.Format("您确认要删除卡号{0}吗?", DataBinder.Eval(e.Row.DataItem, "CardNum")) + "')");
                }
                //if (BtnUpdate.Text.Equals("更新"))
                //{
                //    BtnUpdate.Attributes.Add("onclick", "javascript:return confirm('" + string.Format("您确认要更新卡号{0}吗?", DataBinder.Eval(e.Row.DataItem, "CardName")) + "')");
                //}
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox CheckSingle = e.Row.Cells[0].FindControl("CheckSingle") as CheckBox;
                CheckSingle.Attributes.Add("onclick", "javascript:changecolor(this,'" + e.Row.ClientID + "')");            e.Row.Attributes.Add("onmouseover", "CurrentColor=this.style.backgroundColor;this.style.backgroundColor='#B9D3FF';this.style.cursor='hand';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=CurrentColor");
            }
        }
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindList();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //批量删除
            int intCount = this.GridView1.Rows.Count;
            for (int i = 0; i < intCount; i++)
            {
                CheckBox CheckSingle = this.GridView1.Rows[i].Cells[0].FindControl("CheckSingle") as CheckBox;
                if (CheckSingle.Checked)
                {
                    int CardID = Convert.ToInt32(this.GridView1.Rows[i].Cells[1].Text);
                    Card Cardinfo = new Card();
                    Cardinfo.CID = CardID;
                    Card.DeleteCard(Cardinfo);
                }
            }
            BindList();
            this.GridView1.DataBind();
            //this.GridView1.DataBind();
            //GridView1.PageIndex = -1;
        }
    }
      

  4.   

    <%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Manage/DTDManage.master" CodeFile="CardInfo.aspx.cs" Inherits="Manage_CardInfo" %><%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <script type="text/javascript">
    function changecolor(cbo,o)
    {
      var theBox=cbo;
      var tr=document.getElementById(o);
      if(theBox.checked)
      {
        tr.style.backgroundColor="#B9D3FF";
      }
      else
      {
        tr.style.backgroundColor="#FFFFFF";
        
      }
    }
    function SelectAll(tempControl)
    {
       var theBox=tempControl;
       var xState=theBox.checked;
       var elem=theBox.form.elements;
       for(i=0;i<elem.length;i++)
       {
         if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
         {
           if(elem[i].checked!=xState)
           {
             elem[i].click();
           }
         }
       } }
    </script>
        <table border="0" width="90%" align="left" cellpadding="2" cellspacing="1"  style="margin-left:30px; margin-top:30px;">
            <tr>
                <td align="center" colspan="6" background="../images/bj.bmp" style="height: 23px">
                    <asp:Label ID="Label1" runat="server" Text="卡号信息"></asp:Label></td>
            </tr>
            <tr>
                <td style="height: 12px;" align="center" colspan="6">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        DataKeyNames="CID"  ForeColor="#333333" GridLines="None"
                        OnRowDataBound="GridView1_RowDataBound" Width="100%" PageSize="8" OnRowDeleting="GridView1_RowDeleting" >
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <Columns>
                            <asp:TemplateField>
                                  <HeaderTemplate>
                               
                                    <input id="CheckAll" type="checkbox" onclick="SelectAll(this)"; /><%--   <asp:CheckBox ID="CheckAll" runat="server" onclick="SelectAll(this)"; />--%>
                                </HeaderTemplate>
                                <ItemTemplate>
                                 <%--   <input id="CheckSingle" type="checkbox" />--%>
                                 <asp:CheckBox ID="CheckSingle" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="CID" HeaderText="编号" InsertVisible="False" ReadOnly="True"
                                SortExpression="CRID" >
                                <HeaderStyle Font-Size="12px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="CardNum" HeaderText="卡号" SortExpression="CardName" ReadOnly="True">
                                <ControlStyle Width="100px" />
                                <HeaderStyle Font-Size="12px" />
                            </asp:BoundField>
                            <asp:CommandField ShowDeleteButton="True" />
                        </Columns>
                        <RowStyle BackColor="#EFF3FB" />
                        <EditRowStyle BackColor="#2461BF" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" />
                    </asp:GridView>
              
                </td>
            </tr>
            <tr>
                <td colspan="2" Width="100px" style="height: 24px" align="center" >
                    <asp:Button ID="Button1" runat="server" Text="删除" OnClick="Button1_Click" /></td>
                <td colspan="4" style="height: 24px" align="left" width="300px">
                    <webdiyer:aspnetpager id="AspNetPager1" runat="server" AlwaysShow="True" PageSize="6" OnPageChanging="AspNetPager1_PageChanging" ></webdiyer:aspnetpager>
                  
                  
                </td>
            </tr>
            <tr>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
            </tr>
        </table>
    </asp:Content>
      

  5.   

    using System;
    using System.Data;
    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;
    using System.Data.SqlClient;public partial class HR_HRAdmin_AppCheck : System.Web.UI.Page
    {
          
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        public void bind()
        {
            string cmdtxt1 = "server=(local);database=HrSQL;Uid=sa;Pwd=fov152587018";
            SqlConnection Con = new SqlConnection(cmdtxt1);
            Con.Open();
            string sql = "select   *   from   [CHECKWORK]   where  1=1   ";
            SqlDataAdapter Da = new SqlDataAdapter(sql, Con);
            DataSet ds = new DataSet();
            Da.Fill(ds);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            bind();
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;database=HrSQL;uid=sa;pwd=fov152587018");
            con.Open();
            SqlCommand com = new SqlCommand("delete from [CHECKWORK] where id='" + GridView1.DataKeys[e.RowIndex].Value + "'", con);
            com.ExecuteNonQuery();
            SqlDataAdapter ada = new SqlDataAdapter("select * from [CHECKWORK] order by id desc", con);
            DataSet ds = new DataSet();
            ada.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            con.Close();
            
            bind();
        }
    }
      

  6.   

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
        { 
            GridView1.PageIndex = e.NewPageIndex; 
            GridView1.DataBind(); 
            bind();//解决