It has been a few days since my last update. This was mostly due to some issues at work that needed to be dealt with, leaving me without time to do much development on my own site. Now that most of the important issues have been fixed, I had some time to take a look at a small nagging issue that I was experiencing with my CMS.
As part of the CMS, there is a list with all articles, showing subject, created/modified info, but also state, version and role information. The roles that are allowed to view the article are displayed with checkboxes. The checkboxes have an auto postback attribute which allows for quick editing. If you want to edit an article, you can click the subject and a new window will open with a WYSIWYG editor, a textbox for the subject and a list of checkboxes for the allowed roles.
The goal was to have a single button to save the content, close the window and trigger a refresh of the articles list.
The part in the CMS where you open the window looks something like this (pseudo JavaScript code) : ShowModalPopUp(); Refresh();
The ShowModalPopup() function causes a new window to open, loading the article in the WYSIWYG editor, and setting the current subject and allowed roles. So far, so good. Then, after editing the article, there is a save button with the following code:
btnCreateArticle.Attributes["onclick"] = "javascript:" + Page.ClientScript.GetPostBackEventReference(btnCreateArticle, "ModalDialog", false) + ";CloseWindow();";
The Page.ClientScript.GetPostBackEventReference() generates a JavaScript code that follows the ASP.NET rules and, if the control that you use as a reference is declared as an asynchronous postback control, will trigger an AJAX postback (why reinvent the wheel if you don't have to?). To capture this event, I override the RaisePostBackEvent() of the form. What I found out that for some reason, the article didn't always get saved. On other occasions, only part of the Articles list would update after cussing a bit, rewriting id's of the checkboxes to incorporate the article version for force an update after the article was saved and blaming the viewstate for somehow restoring values and refusing to update my checkboxes, I found out all problems were solved after just calling base.RaisePostBackEvent in the override part. Sorry viewstate!
protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
{
if (sourceControl == btnCreateArticle && eventArgument == "ModalDialog")
{
if (EditArticleID == 0)
{
//Create new article
}
else
{
//Update article
}
// Here is where I forgot to call base.RaisePostBackEvent....After adding it, all issues were resolved.
}
else
{
base.RaisePostBackEvent(sourceControl, eventArgument);
}
}