Code for Deleting a Page

Apr 18, 2008 at 12:46 AM
Some people previously commented on creating this since. Here's one way of doing it: When editing the page name, you have a text box showing the new name of the page and a SAVE button next to it. I added a DELETE button next to the save button. Then we need to hook up the Delete button click to the middleware worklow to delete all current widget instances on the page and then delete the page itself.
We only display the delete button if the current page is not the last page since we dont want to delete all pages.

1. Change default.aspx to create the Delete button:

void PageTitleEditMode_Click(object sender, EventArgs e)
{
...
linkButton.Parent.Controls.Add(editTextBox);
linkButton.Parent.Controls.Add(okButton);

//Allow the user to remove this page if its not the last page
var setup = Context.Itemstypeof(UserPageSetup) as UserPageSetup;
if (setup.Pages.Count > 1)
{
var deleteButton = new Button();
deleteButton.ID = "tButtonPageDelete";
deleteButton.Text = "Delete";
linkButton.Parent.Controls.Add(deleteButton);
}
linkButton.Parent.Controls.Remove(linkButton);
}

2. Change default.aspx to process the delete button click:

protected override void CreateChildControls()
{
...
if( ScriptManager1.IsInAsyncPostBack )
{
string updatePanelName = RequestScriptManager1.ID;

if( !updatePanelName.Contains(TabUpdatePanel.ID) )
this.SetupWidgets( wi => false );
else
{
//is this an asynch callback from trying to delete the page
if (updatePanelName.Contains("tButtonPageDelete"))
{
this.DeletePageAndWidgets();
this.ReloadPage(wi => true);
this.RefreshAllColumns();
return;
}

//this an asynch callback from changing the page name
string pageName = Request"PageNameEditTextBox";
if( !string.IsNullOrEmpty(pageName) )
{
new DashboardFacade(Profile.UserName).ChangePageName(pageName);
_Setup.CurrentPage.Title = pageName;
this.SetupTabs();
}
}



3. Add DeletePageAndWidgets method to Default.aspx:

private void DeletePageAndWidgets()
{
var setup = Context.Itemstypeof(UserPageSetup) as UserPageSetup;
var columnPanels = new Panel[] {
this.FindControl("LeftPanel") as Panel,
this.FindControl("MiddlePanel") as Panel,
this.FindControl("RightPanel") as Panel };
foreach (WidgetInstance instance in setup.WidgetInstances)
{
new DashboardFacade(Profile.UserName).DeleteWidgetInstance(instance);
}
new DashboardFacade(Profile.UserName).DeleteCurrentPage(setup.CurrentPage);
}

4. Add the new method DeleteCurrentPage to DashboardFacade:
public void DeleteCurrentPage(Page instance)
{
using (new TimedLog(this._UserName, "Delete Page: " + instance.Title))
{
var properties = new Dictionary<string, object>();
properties.Add("UserName", this._UserName);
properties.Add("PageId", instance.ID);
WorkflowHelper.ExecuteWorkflow(typeof(DeletePageWorkflow), properties);
}
}

5. Add DeletePageWorkflow to the current list of Workflows. Heres the code behind. It uses the enw activity called DeletePageActivity.
namespace DashboardBusiness
{
partial class DeletePageWorkflow
...
private DashboardBusiness.Activities.GetUserGuidActivity getUserGuidActivity;
private DashboardBusiness.Activities.DeletePageActivity deletePageActivity;
}
}

6. Add a new Activity called DeletePageActivity. Its similar to the other activities:

namespace DashboardBusiness.Activities
{
public partial class DeletePageActivity : System.Workflow.ComponentModel.Activity
{
public DeletePageActivity()
{
InitializeComponent();
}
private static DependencyProperty UserGuidProperty = DependencyProperty.Register("UserGuid", typeof(Guid), typeof(DeletePageActivity));

public Guid UserGuid
{
get { return (Guid)base.GetValue(UserGuidProperty); }
set { base.SetValue(UserGuidProperty, value); }
}

private static DependencyProperty PageIdProperty = DependencyProperty.Register("PageId", typeof(int), typeof(DeletePageActivity));
public int PageId
{
get { return (int)base.GetValue(PageIdProperty); }
set { base.SetValue(PageIdProperty, value); }
}

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{

using (var db = DatabaseHelper.GetDashboardData())
{
var candidatePage = (from page in db.Pages
where page.UserId == UserGuid
orderby page.ID ascending
select page).First();

var tUserSettings = db.UserSettings.Single(userSetting => userSetting.UserId == this.UserGuid);
tUserSettings.CurrentPageId = candidatePage.ID;
db.SubmitChanges();

var pageToDelete = db.Pages.Single(p => p.ID == PageId);
db.Pages.DeleteOnSubmit(pageToDelete);
db.SubmitChanges();
}
return ActivityExecutionStatus.Closed;
}
}
}