+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Get Inserted PageID in Save button click

Get Inserted PageID in Save button click

14 posts, 1 answered
  1. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    15 Jul 2010
    Link to this post
    Hello Team Telerik,

    I am creating a new page with  a custom field inside it. I am able to get the custom field value but the inserted page ID is not available to me. I tried using this.PageEditor.SelectedPageId but gives the value of the parent page instead of the child page.

    Tried referring the below article:

    http://www.sitefinity.com/devnet/forums/sitefinity-3-x/developing-with-sitefinity/320180-custom-page-properties.aspx

    which allows to use PagePropertiesEdit.ascx, but getting the value or retrieving the value is not an issue currently. But only the inserted PageID id not available on Save Button click event.

    Regards,
    Siddesh Kapadi
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    15 Jul 2010
    Link to this post
    Hi Siddesh Kapadi,

    The following code gets the ID of the page that is being created or edited.

    Guid pageID = ((Telerik.Cms.Web.UI.PageEditor)(this.Parent.Parent.Parent.Parent.Parent.Parent)).SelectedPageId;

    The code should be added inside OnPreRender of PagePropertiesEdit template.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    15 Jul 2010
    Link to this post
    Hello,
    The code that has been provided to be placed in PagePropertiesEdit.ascx.cs file can also be placed in Pages.aspx.cs page with this.PageEditor.SelectedPageId.And this is the way I have used it while editing the selected page.

    But earlier and my current question is:

    How do I get the Inserted PageId when I am adding a new Page (i.e. I am not editing the already created page).

    Regards,
    Siddesh Kapadi
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    15 Jul 2010
    Link to this post
    Hi Siddesh Kapadi,

    There are two options that are suggestion in the post you are referring.

    1. Option PagePropertiesEdit.ascx
    2. Subscribing for CmsManager.Executing

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    15 Jul 2010
    Link to this post
    Hello Team Telerik,

    Thanks for the two option provided. But no thanks as the referring post does not satisfy my requirements.

    Is PagePropertiesEdit.ascx used while creating the page. If yes, then what is the use of control pagepropertiesInsert.ascx?

    Also, can I get to know how can I subscribe to the CmsManager.Executing event?

    Thanks in anticipation of the answer.

    Regards,
    Siddesh Kapadi
  6. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    15 Jul 2010
    Link to this post
    Hi Siddesh,

    I have had the same problem as you. I solved it by subscribing to the CmsManager's Executed event as Ivan has suggested. The problem with this is that this seems to run several times when creating a page, to get round this I store a reference to the created page and then use this in the OnUnload method to do the processing on my custom field.

    Here is my code behind from PagePropertiesInsert.ascx...

    private CmsPage CreatedPage { get; set; }

    protected override void OnInit(EventArgs e)
    {
    CmsManager.Executed += CmsManager_Executed;
    base.OnInit(e);
    }
     
    void CmsManager_Executed(object sender, Telerik.ExecutedEventArgs e)
    {
    if (e.CommandName.Equals("CreatePage"))
    CreatedPage = e.Data as CmsPage;
    }
     
    protected override void OnUnload(EventArgs e)
    {
         if (CreatedPage != null)
         {
    // do some processing
         }
    }


    Hope this helps you out.

    Chris.


    Answered
  7. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    16 Jul 2010
    Link to this post
    Hey Chris,

    Thank a ton.

    Regards,
    Siddesh Kapadi
  8. Al
    Al avatar
    33 posts
    Registered:
    28 Dec 2008
    01 Aug 2010
    Link to this post
    Hi Siddesh/Chris,

    I've looked at the suggestion Chris provided which seems to be a very good one. I've got custom fields in the AdminPropertiesEdit.ascx and AdminPropertiesInsert.ascx, however what I'm not clear on is how i then store the value of my custom field, along with the pageID in the database. Am i literally writing a sql statement, or am i writing properties that sitefinity will store for me?

    Any help you can give would be greatly appreciated.

    Thanks
    higgsy
  9. Siddesh Kapadi
    Siddesh Kapadi avatar
    239 posts
    Registered:
    09 Oct 2009
    02 Aug 2010
    Link to this post
    Hey Higgsy,

    Hope u are getting the values from the custom fields, you need to do the processing in the OnUnload Event and u need to write the custom DAL and custom table needs to be created to store the data.

    Hope this answers your question.

    Regads,
    Siddesh Kapadi
  10. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    16 Aug 2010
    Link to this post
    Just an update to my solution...

    I am using similar logic to remove the custom properties when a page is deleted. I have this code in the Admin.master code behind. It needs updating however, as when you delete a page with child pages the custom properties for the child page remain behind in the database...

    private CmsPage DeletedPage { get; set; }
     
    private void CmsManager_Executed(object sender, Telerik.ExecutedEventArgs e)
    {
     // Catch page deletions so we can delete our custom properties.
     if (e.CommandName.Equals("DeletePage"))
    DeletedPage = e.Data as CmsPage;
    }
     
    protected override void OnUnload(EventArgs e)
    {
         if (DeletedPage != null)
            {
                 // Delete custom properties for this page

                 // TODO: Delete child page custom properties
    }
    }

  11. Dan
    Dan avatar
    24 posts
    Registered:
    02 Oct 2012
    18 Apr 2012
    Link to this post
    Chris,

    Would you be willing to post your complete solution (minus your DAL code)?  Seems like this is a common question, and the solution is scattered over a number of posts.

    Thanks!
    DanO
  12. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    19 Apr 2012
    Link to this post
    Hi Dan,

    There's not much too it that I haven't already posted above.

    In Sitefinity\Admin\ControlTemplates\Pages\PagePropertiesInsert.ascx I have added some textboxes and other controls to capture custom data, and in the code behind of this control I am saving it on the OnUnload method.

    private CmsPage CreatedPage { get; set; }
     
    protected override void OnInit(EventArgs e)
    {
    CmsManager.Executed += CmsManager_Executed;
    base.OnInit(e);
    }
      
    void CmsManager_Executed(object sender, Telerik.ExecutedEventArgs e)
    {
    if (e.CommandName.Equals("CreatePage"))
        CreatedPage = e.Data as CmsPage;
    }
      
    protected override void OnUnload(EventArgs e)
    {
         if (CreatedPage != null)
         {
        var custom1 = TextBox1.Text;
        var custom2 = TextBox2.Text;
            CustomFieldDAL.Save(CreatedPage.ID, CreatedPage.LangID, custom1, custom2);
         }
    }

    For editing the custom fields, in PagePropertiesEdit.ascx are similar controls and the code behind...

    private bool ChangeSubmitted { get; set; }
    private CmsPage UpdatedPage { get; set; }
     
    protected override void OnInit(EventArgs e)
            {
                CmsManager.Executed += CmsManager_Executed;
                base.OnInit(e);
            }
     
            protected override void OnPreRender(EventArgs e)
            {
                if (!ChangeSubmitted)
                {
                    // checking the ChangeSubmitted property stops new values being overwritten
                    var pageId = GetEditingPageId();
                    TextBox1.Text = CustomFieldDAL.GetCustom1(pageId);
                    TextBox2.Text = CustomFieldDAL.GetCustom2(pageId);
                }
     
                base.OnPreRender(e);
            }
     
    // I've added this submit event to the save button so I know when the page is being saved
    protected void Submit(object sender, EventArgs e)
            {
                ChangeSubmitted = true;
            }
     
    protected override void OnUnload(EventArgs e)
            {
                if (UpdatedPage != null)
                {
                       var pageId = GetEditingPageId();
                       var custom1 = TextBox1.Text;
                       var custom2 = TextBox2.Text;
                       CustomFieldDAL.Update(UpdatedPage.ID, UpdatedPage.LangID, custom1, custom2); 
                }
    }
    private void CmsManager_Executed(object sender, Telerik.ExecutedEventArgs e)
            {
                if (e.CommandName.Equals("UpdatePage"))
                    UpdatedPage = e.Data as CmsPage;
            }
     
    private Guid GetEditingPageId()
            {
                return !string.IsNullOrEmpty(Request["select"]) ? new Guid(Request["select"]) : ((Telerik.Cms.Web.UI.PageEditor)(this.Parent.Parent.Parent.Parent.Parent.Parent)).SelectedPageId;
            }

    In the end I put my delete logic in Global.asax

    void Application_Start(object sender, EventArgs e)
            {
                // Code that runs on application startup
                CmsManager.Executed += CmsManager_Executed;
     
            }
     
            void CmsManager_Executed(object sender, Telerik.ExecutedEventArgs e)
            {
                if (e.CommandName.Equals("DeletePage"))
                {
                    CustomFieldDAL.DeleteAll((CmsPage)e.Data);
                }
            }

  13. Dan
    Dan avatar
    24 posts
    Registered:
    02 Oct 2012
    19 Apr 2012
    Link to this post
    Thanks Chris! So why did you end up putting the delete in the global.asax?
    Also - I'm curious to know if you (or anyone on this post) explored extending the Sitefinity data model, by adding a column (or columns) to the sf_CmsPageProps table, which seems like where the data belongs.
  14. Dan
    Dan avatar
    24 posts
    Registered:
    02 Oct 2012
    19 Apr 2012
    Link to this post
    A follow up on this - I ended up adding a column to sf_CmsPageProps, and it seems to be working out fine.  The nice thing about it is that I didn't have to add any code to handle the delete operation.
Register for webinar
14 posts, 1 answered