User and Role Management for an ASP.NET AJAX Server-Centric Based Online Shopping Website - Deleting the Selected User
(Page 5 of 6 )
Deleting the Selected User
When clicking the "X" within the GridView control, the deleting function is triggered to delete the current user.
protected void UserView_RowDataBound(object sender,GridViewRowEventArgs e)
{ ///find the 'Delete' button
ImageButton deleteBtn = (ImageButton)e.Row.FindControl("DeleteBtn");
if(deleteBtn != null)
{ ///add the dialog that confirms the user to make the deletion
deleteBtn.Attributes.Add("onclick","return confirm('Are you sure to delete the selected items?');");
}
}
protected void UserView_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName == "delete"){
///delete data
User user = new User();
user.DeleteUser(Int32.Parse(e.CommandArgument.ToString()));
///rebind data to the control
BindUserData();
Response.Write("<script>alert('" + "Deleting successfully. Please safekeep your data!" + "');</script>");
}
}
Note the the RowDataBoundand RowCommandevent handlers are very important and typical functions in handling the GridView control. Here, we first find the deleting related ImageButton by calling the GridViewRowEventArgs.Row.FindControl. Then, we attach a client-side "confirm" dialog box to the "deleteBtn" button. Now clicking the "X" hyperlink within the GridView control triggers the UserView_RowCommand eventin which we first find the special "delete" command, then perform the deletion by calling the DeleteUser member methodof the User class, and finally update the GridView control and display to the user a friendly hint for this operation.
Next: Adding Background Users >>
More ASP.NET Articles
More By Xianzhong Zhu