当前位置:编程学习 > C#/ASP.NET >>

asp.net 全选择删除后数据绑定问题!!



<div>
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" PageSize="2" Width="625px">
              <Columns>
                <asp:TemplateField HeaderText="delete">
                      <ItemTemplate>
                          <asp:CheckBox    ID="del" runat="server" />
                      </ItemTemplate>
                  </asp:TemplateField>
                  <asp:BoundField DataField="Id" HeaderText="编号" >
                    <ItemStyle CssClass="hidden" />
                      <HeaderStyle CssClass="hidden" />
                      <FooterStyle CssClass="hidden" />
                  </asp:BoundField>
                  <asp:BoundField DataField="Atitle" HeaderText="产品类名" />
                  <asp:BoundField DataField="UsesName" HeaderText="描述信息" />
              </Columns>
              <HeaderStyle BackColor="#EDF7E7" Font-Bold="True" />
              <RowStyle BackColor="#F9F9F9" />
              <AlternatingRowStyle BackColor="White" />
          </asp:GridView>
          <br />
          <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="全选" />
          <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="删除" /></div>





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.OleDb;

public partial class Admin_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadGridView();
        }
        Button1.Attributes.Add("onclick", "return confirm('are you sure?')");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql = "delete from Categories where";
        string cal = "";
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("del");
            if (cb.Checked == true)
            {
                cal += " Id=" + GridView1.DataKeys[i].Value.ToString() + " or";
            }
        }
        if (cal != "")
        {
            sql += cal.Substring(0, cal.Length - 3);
        }
        else
        {
            sql = "";//不删除
        }
        //Response.Write(sql);//这里可以自己定义程序,进行删除任务
        int Id = Convert.ToInt32(GridView1.DataKeys[0].Value);
        Add_index Del = new Add_index();
        Del.Dele_index(Id);
        this.GridView1.DataBind();
    }
    private void loadGridView()
    {
        OleDbConnection conn = Connection.Dataconnection();
        DataSet ds = new DataSet();       
        OleDbDataAdapter da = new OleDbDataAdapter("select * from A_Index", conn);
        da.Fill(ds, "A_Index");
        GridView1.DataSource = ds.Tables["A_Index"];
        GridView1.DataKeyNames = new string[] { "Id" };
        GridView1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        CheckBox cb;
        if (Button2.Text == "全选")
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("del");
                cb.Checked = true;
            }
            Button2.Text = "取消";
        }
        else
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("del");
                cb.Checked = false;
            }
            Button2.Text = "全选";
        }
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {      
        int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
        Add_index Del = new Add_index();
        Del.Dele_index(Id);
        this.GridView1.DataBind();
    }
}


删除后,GridView1显示为空白!!  而且单击第二页的时候也为空白!!! 这是怎么回事!! --------------------编程问答-------------------- 怎么没有人啊!!! --------------------编程问答-------------------- 帮帮我啊,急啊!!! --------------------编程问答-------------------- 哪位大哥,求求你们了。。。 --------------------编程问答-------------------- 兄弟啊,你那个Oledb是连Access数据库的啊,你咋的用在这里啊,连接字符串格式是Provider="microsoft.jet.4.0.oledb,data source=" + Server.MapPath(".mdb")"; --------------------编程问答-------------------- 呵呵,兄弟,这样可以呢!!! 我都是这样用的呢!!!!  问题自己解决了。。。
谢谢大家了。。。
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,