Anda di halaman 1dari 3

Adding Controls to a Grid View

You can delete or edit the particular record in the grid view.
For each control specify the command type as edit or delete.
Now handle the gridview_rowcommand event for the gridview.

In the rowcommand event depending on the rowcommand type whether edit or delete
You can perform the required task.

protected void grvCourses_RowCommand(object sender,


GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
long CourseId = long.Parse(e.CommandArgument.ToString());
//write your code here to delete this record
}
else if (e.CommandName == "Edit")
{
double CourseId =
Convert.ToDouble(e.CommandArgument.ToString());
//write your code here to edit the record
}
}
For viewing the details of a particular record make the details field as link and get the link
value from the back end by appending the primary key of the grid view as query string.
2

For example if you view the second row details in the above grid
Here the coursed is passed in the query string so that you can access the course details in this
page as it is the primary key of the grid.

Now you can add a confirmation message while deleting the record.
In the dataBound event add the JavaScript code that shows a confirmation popup for the
delete column.
This is done using the below code

protected void grvCourses_RowDataBound(object sender, GridViewRowEventArgs


e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton img =
(ImageButton)e.Row.FindControl("imgBtnDelete");
img.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete this Course: " +
DataBinder.Eval(e.Row.DataItem, "CourseId") + "')");
}
3

Download the source code in ASP.NET using C# from the below link
http://docs.google.com/leaf?
id=0B8hoTwnI_pgYZDBhMTQ0ZjEtNDk0My00Y2YwLThmNmYtMWU4ZTVjMTIwZm
Q0&hl=en

Anda mungkin juga menyukai