همانطور که میدانید GridView جزء جداناپذیر از اکثر پروژههای برنامه نویسان ASP.NET Web forms میباشد. اکثرا روشی که در میان برنامه نویسان بیشتر استفاده میشود، قرار دادن یک دکمه/لینک در هر ردیف از GridView برای حذف رکورد مورد نظر میباشد. در این مقاله قصد دارم روشی را ارائه کنم تا کاربر قادر باشد هر تعداد رکورد را که مدنظر دارد، انتخاب کرده و با فشردن دکمه "حذف" رکوردهای انتخاب شده را حذف کند.
برای درک بهتر، ابتدا جدولی به اسم "Emploee" را در SQL Server با مشخصات زیر ساخته :
CREATE TABLE [dbo].[Employee] (
[EmpId] INT NOT NULL,
[FirstName] VARCHAR (20) NOT NULL,
[LastName] VARCHAR (20) NOT NULL,
[City] VARCHAR (20) NOT NULL,
PRIMARY KEY CLUSTERED ([EmpId] ASC)
);
1- یک GridView به صفحه افزوده و خاصیت
AutoGenerateColumns آن را برابر False قرار دهید . 2- فیلدهایی را که قصد نمایش آنها در GridView را دارید به صورت زیر به GridView بیفزایید :
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
3- برای قرار دادن کنترلهای Asp.net که در اینجا منظور CheckBox میباشد میبایست از
TemplateField و قرار دادن تگ ItemTemplate درون آن، به صورت زیر استفاده نمایید : <asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkDel" runat="server" />
</ItemTemplate>
</asp:TemplateField>
و بعد از تگ GridView دکمهای را برای حذف موارد انتخابی در فرم قرار دهید :
<asp:Button ID="btnDeleteRecord" runat="server" OnClick="btnDeleteRecord_Click" Text="Delete" />
برای نمایش یک پیغام به کاربر به منظور Confirm کردن دستور حذف در سمت کلاینت، قطعه کد Javascript زیر را قرار میدهیم:
function DeleteConfirm()
{
var Ans = confirm("Do you want to Delete Selected Employee Record?");
if (Ans)
{
return true;
}
else
{
return false;
}
}
و در رویداد Page_Load کدهای زیر را جهت نمایش مقادیر در GridView و افزودن تابع فوق به دکمه، قبل از حذف رکوردها میافزاییم :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Displaying the Data
showData();
//Adding an Attribute to Server Control(i.e. btnDeleteRecord)
btnDeleteRecord.Attributes.Add("onclick", "javascript:return DeleteConfirm()");
}
}
//Method for Displaying Data
protected void showData()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter adapt = new SqlDataAdapter("select * from Employee",con);
con.Open();
adapt.Fill(dt);
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
ابتدا تابع DeleteRecode را به صورت زیر پیاده سازی میکنیم :
که یک پارمتر را از ورودی دریافت میکند که ID رکورد انتخاب شده میباشد و با استفاده از ID، رکورد مورد نظر را حذف میکنیم :
protected void DeleteRecord(int empid)
{
SqlConnection con = new SqlConnection(cs);
SqlCommand com = new SqlCommand("delete from Employee where EmpId=@ID",con);
com.Parameters.AddWithValue("@ID",empid);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
و اما بخش مهم مربوط به رویداد دکمه میباشد. در هنگام کلیک بر روی دکمه باید تمامی رکوردهای GridView را چک و تمامی رکوردهایی را که CheckBox آنها تیک خورده است گرفته و ID رکورد مورد نظر را به تابع DeleteRecode فرستاد و در پایان برای اعمال تغییرات، متد ShowDate را فراخوانی و GridView را مجددا Bind میکنیم.
protected void btnDeleteRecord_Click(object sender, EventArgs e)
{
foreach (GridViewRow grow in GridView1.Rows)
{
//Searching CheckBox("chkDel") in an individual row of Grid
CheckBox chkdel = (CheckBox)grow.FindControl("chkDel");
//If CheckBox is checked than delete the record with particular empid
if(chkdel.Checked)
{
int empid = Convert.ToInt32(grow.Cells[1].Text);
DeleteRecord(empid);
}
}
//Displaying the Data in GridView
showData();
}