• GridView批量删除记录、全选及弹出确认对话框


    GridView批量删除      GridView添加主键       全选记录       确认删除

              GridView控件允许用户自己编辑模板,这样它就可以象以前混合编程一样任意的添加功能、定义格式等,对于大量记录来说如果一条一条的删除是件累人的工作。对其添加批量删除功能是件很自然的事。在进行工作之前,必须搞明白GridView主键的问题。

            1.GridView主键

               如果将数据源的所有东西都显示在GirdView中,不是件明智的事,最起码主键是应该隐藏的。这样在进行编辑时,也就不需要指定readonly等属性,显示效果也会更理想(相信很多主键值应该是数字编号)。如果隐藏了主键(编辑列设置Visible=false),我们肯定没有办法获取使用类似于这样的语句获取主键值GridView1.Rows[i].Cells[0].Text。在DataGrid时代这种方法是主流的。但是在GridView中这种方法行不同,但是2.X提供了DataKeys和DataKeyNames属性,这比DataGrid更加方便。

              设定一个css样式.hidden{display:none},将其指定给主键的HeaderStyle、FooterStyle及ItemStyle的CssClass属性。那么主键列在运行时就不会被显示。在绑定数据源时需要指定GridView控件的主键:

              GridView1.DataSource = ds.Tables["Categories"];
            GridView1.DataKeyNames = new string[] { "CategoryID" };
              GridView1.DataBind();

              在使用时就很简单了:GridView1.DataKeys[i].Value.ToString(),该语句返回主键值,其中i为当前选中行的行号。现在就可以使用这种方法进行批量删除了。

            2.批量删除

              在进行批量删除时,需要一列选择框,让用户进行选择删除的条目。此时只需要在GridView的第一列或组后一列添加一个<asp:TemplateField >列就可以了。在里面可以编辑模板添加需要的控件。具体的html代码如下所示。

            <asp:GridView ID="GridView1" runat="" AutoGenerateColumns="False">
                  <Columns>
                    <asp:TemplateField HeaderText="delete">
                          <ItemTemplate>
                              <asp:CheckBox    ID="del" runat="server" />
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:BoundField DataField="CategoryID" HeaderText="编号" >
                        <ItemStyle. CssClass="hidden" />
                          <HeaderStyle. CssClass="hidden" />
                          <FooterStyle. CssClass="hidden" />
                      </asp:BoundField>
                      <asp:BoundField DataField="CategoryName" HeaderText="产品类名" />
                      <asp:BoundField DataField="Description" HeaderText="描述信息" />
                  </Columns>
                  <HeaderStyle. BackColor="#EDF7E7" Font-Bold="True" />
                  <RowStyle. BackColor="#F9F9F9" />
                  <AlternatingRowStyle. BackColor="White" />
              </asp:GridView>
              <br />
              <asp:Button ID="Button1" runat="server" nClick="Button1_Click" Text="删除" />
              <asp:Button ID="Button2" runat="server" nClick="Button2_Click" Text="全选" />

    </div>

            3.删除前确认
             在这里使用Button1按钮进行删除任务,那在进行删除前需要用户进行最后确认,只需要在page_load事件中添加如下一行程序:
            Button1.Attributes.Add("onclick", "return confirm('are you sure?')");
           4.全选记录
              当用户单击Button2时,如果Text是全选,执行选中所有的行,并设定该按钮为取消,此时再次单击Button2,就取消所有选择。代码如下
          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 = "全选";
              }
          }
          其中del是CheckBox的ID值。

            5.全部源码

    public partial class delAll : System.Web.UI.Page
    {
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
                  loadGridView();
              }
            Button1.Attributes.Add("onclick", "return confirm('are you sure?')");
          }
          protected void Button1_Click(object sender, EventArgs e)
          {
              string  = "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 += " CategoryID=" + GridView1.DataKeys[i].Value.ToString() + " or";
                  }
              }
            if (cal != "")
              {
                  sql += cal.Substring(0, cal.Length - 3);
              }
              else
              {
                sql = "";//不删除
              }
            Response.Write(sql);//这里可以自己定义程序,进行删除任务
          }
          private void loadGridView()
          {
              string strconn = "server=localhost\sqlexpress;database=Northwind;user id=rayshow;password=ray123";
              SqlConnection conn = new SqlConnection(strconn);
              DataSet ds = new DataSet();
              SqlDataAdapter da = new SqlDataAdapter("select * from Categories", conn);
              da.Fill(ds, "Categories");
              GridView1.DataSource = ds.Tables["Categories"];
            GridView1.DataKeyNames = new string[] { "CategoryID" };
              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 = "全选";
            }
          }
    }

  • 相关阅读:
    SQL Data Services Abandons REST for TDS API and Knocks My Socks Off
    C# Lambda Expressions 简介
    Web.config 和 App.config 区别
    XPO 第三方控件学习(DevExpress Persistent Object )系列查询
    进销存管理系统 系列
    MOSS 学习系列(十万个为什么)--为什么我要定制审批流程的时候找不到审批的选项?
    XPO 第三方控件学习(DevExpress Persistent Object )系列 其他(session,事务,有效性检查...
    如何使用三态工作流(一)
    english interview example
    How to build a K2 SharePoint Workflow(2)
  • 原文地址:https://www.cnblogs.com/czsl/p/3251405.html
Copyright © 2020-2023  润新知