Sitecore 5.3 HandleMessage and ShowModalDialog

Creating your own Sitecore Fields can be tricky. The best way to start is to download one of Sitecores examples (the composite control is a good example).
In this example I’ll use the HandleMessage function in my control to open a modal window, do some stuff and return the values to my control.

The HandleMessage function reacts on events from the Sitecore Client. Each message contains the ID of the current control, which I will use to react to messages that belongs to me:

public override void HandleMessage(Sitecore.Web.UI.Sheer.Message message)
  if (message["id"] == this.ID)
    if ( message.Name == "MyControl:message" )
      System.Web.HttpContext.Current.Session["MyControl.ID"] = this.ID;
      ClientCommand command = Sitecore.Context.ClientPage.ClientResponse.ShowModalDialog("MyPage.aspx", "300px", "630px", "my:Message", false);

The trick is now that when the modal dialog closes, a message called “my:Message” will be thrown. Unfortunately there is no control ID attached to the message. In order to identify which control opened the modal window, I store the control’s ID in a session variable, and use this ID when handling the my:Message message:

if ( message.Name == "my:Message" )
  string id = System.Web.HttpContext.Current.Session["MyControl.ID"] as string;
  if (id == this.ID)
    // do my work, and remember to clear the session

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 5 and tagged , , , . Bookmark the permalink.

1 Response to Sitecore 5.3 HandleMessage and ShowModalDialog

  1. Pingback: Sitecore Custom Field – Image Coordinate Picker – Walking on clouds

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.