我有一个 GridView,其中包含从 TextBox 中提取的多行数据(使用会话)。GridView 上应该有一列包含每行上的 CheckBox,以选择要删除的行。我在 GridView 之外有一个按钮。单击它后,GridView 中的所有选定行(使用 CheckBox)都应从 GridView 中“删除”。我不想要 GridView 上的“删除链接”。
请对下面给出的代码进行必要的更改以实现此功能。
默认.aspx
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Add to Grid" OnClick="Button1_Click" />
<br />
<asp:GridView ID="GridView1" runat="server" DataKeyNames="EmpID" AutoGenerateColumns="false"
OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging"
PageSize="5" AllowPaging="true" OnRowUpdating="GridView1_RowUpdating" Width="800">
<Columns>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<%#Eval("EmpID")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<%#Eval("EmpName")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpName" runat="server" Text='<%#Eval("EmpName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<%#Eval("EmpSalary")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpSalary" runat="server" Text='<%#Eval("EmpSalary") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Modify" ShowEditButton="true" EditText="Edit">
<ControlStyle Width="50" />
</asp:CommandField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" OnClientClick="return confirm('Are you sure you want to delete these records?');">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
**<asp:TemplateField HeaderText="Tick to Delete Rows">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" Text="Select" />
</ItemTemplate>
<HeaderTemplate>
</HeaderTemplate>
</asp:TemplateField>**
</Columns>
</asp:GridView>
</div>
<p style="width: 799px">
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Delete Checked items" Width="162px" /></p>
</body>
默认.aspx.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
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.Linq;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
DataRow dr;
DataRow dr1;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "";
//lbldbmsg.Text = "";
if (!Page.IsPostBack)
{
dt.Columns.Add("EmpID");
dt.Columns.Add("EmpName");
dt.Columns.Add("EmpSalary");
Session["reptable"] = dt;
GridData();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string EmpID;
EmpID = GridView1.DataKeys[e.RowIndex].Value.ToString();
if (Session["reptable"] != null)
{
DataTable dt1 = new DataTable();
dt1.Clear();
dt1 = Session["reptable"] as DataTable;
for (int i = 0; i <= dt1.Rows.Count - 1; i++)
{
DataRow dr;
if (dt1.Rows[i][0].ToString() == EmpID)
{
dr = dt1.Rows[i];
dt1.Rows[i].Delete();
//dt1.Rows.Remove(dr);
}
}
Session.Remove("reptable");
Session["reptable"] = dt1;
}
GridData();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridData();
}
protected void Button1_Click(object sender, EventArgs e)
{
dt = (DataTable)Session["reptable"];
dr = dt.NewRow();
dr["EmpID"] = TextBox1.Text;
dr["EmpName"] = TextBox2.Text;
dr["EmpSalary"] = TextBox3.Text;
dt.Rows.Add(dr);
Session.Remove("reptable");
Session["reptable"] = dt;
GridData();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
protected void Button3_Click(object sender, EventArgs e)
{
}
}