Finishing an Online E-Mail System in ASP.NET 2.0 - Deleting Folders
(Page 2 of 5 )
Deleting folders is accomplished inside the MailDesktop.aspx page. To do so, you can just click the X button on the web page, and then the selected folders will be deleted.
When we use Windows Explorer.exe to delete something, a dialog always appears to remind us of the possible danger. To achieve such an effect ourselves, we add the related code into the RowDataBound event of the FolderView control.
protected void FolderView_RowDataBound(object sender,GridViewRowEventArgs e)
{
ImageButton deleteBtn = (ImageButton)e.Row.FindControl("DeleteBtn");
if(deleteBtn != null)
{
deleteBtn.Attributes.Add("onclick", "return confirm('Are you sure to delete the selected item?');");
}
}
Here, we first search for the button named DeleteBtn. If DeleteBtn is not null (i.e. exists or is found), then we bind a confirming dialog to it.
The main task of making the deletion is done within the GridView's RowCommand event.
protected void FolderView_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName == "delete")
{
try
{ ///delete data
IFolder folder = new Folder();
folder.DeleteFolder(Int32.Parse(e.CommandArgument.ToString()));
///rebind the controls' data
BindFolderData();
Response.Write("<script>alert('" + "Deleting successfully,safekeep your data!" + "');</script>");
}
catch(Exception ex)
{ ///jump to the page dealing with exception handling
Response.Redirect("ErrorPage.aspx?ErrorMsg=" + ex.Message.Replace("<br>","").Replace("n","")
+ "&ErrorUrl=" + Request.Url.ToString().Replace("<br>","").Replace("n",""));
}
}
}
Here, in the RowCommand event, we obtain the value of the CommandName property of the X button and save it in the CommandName parameter. Next, we get the value of the CommandArgument property and convert it into an integer which is stored in the nFolderID parameter. If all the requirements are satisfied, then we start to delete the selected folder, which is fulfilled by calling the DeleteFolder (int nFolderID) function of the IDisk interface, removing the item (whose value of FolderID is nFolderID) from the database. And finally, an appropriate dialog appears to indicate whether or not the operation is successful.
Next: Reading Your Email >>
More ASP.NET Articles
More By Xianzhong Zhu