Home > .net 2.0, .net 3.0, .net 3.5, AJAX, c#, Microsoft, Visual Studio 2005, Visual Studio 2008 > Bubbling up Methods in Composite Controls

Bubbling up Methods in Composite Controls

May 9, 2008

A while back, I wrote a couple of articles (here and here) regarding encapsulating the ModalPopupExtender into a spiffy little template control that you could toss onto a page.  It’s worked GREAT over the past few months, however, I hit a snag today.

I needed to call the base ModalPopupExtender’s .Show() method from code behind; however, I hadn’t bubbled that up to the Composite Control.

At first, I expected to simply add a private instance of the MPE (which is assigned to when the control is built) and then add a method to my composite control that calls the .Show() method.

private ModalPopupExtender _control;

public void Show()

{

       _control.Show();

}

That sounds good, but it never fired and the _control field was always null (even though I could step through and it was assigned).

What it needed was a little reminder—a reminder to EnsureChildControls existed before trying to call Show().  Now, a quick update to the code:

public void Show()

{

       this.EnsureChildControls();

       _control.Show();

}

Now I can call the Show() method of the Composite Control and it works like a charm!  Here’s an example (for what I’m working with at the moment) of dynamically iterating through an IDictionary and returning the values in a Modal Popup.

ASPX:

<tiredstudent:ModalPopupTemplate HeaderText=”ERC” runat=”server”

        ID=”PopupDialogBox” DefaultStyle=”YUI” TargetControlId=”fakeButton” />

<asp:Button ID=”fakeButton” runat=”server” style=”display: none” />

Code-behind:

foreach (var entry in results)

{

       sb.AppendLine(string.Format(“<p>{0} – {1}</p>”,

                     entry.Key, entry.Value));

}

 

PopupDialogBox.BodyText = sb.ToString();

 

PopupDialogBox.Show();

 

  1. May 9, 2008 at 3:24 pm

    If I had $1 for every time EnsureChildControls() solved a problem I’d be doing pretty well.

  2. May 9, 2008 at 6:12 pm

    My joke to myself was the irony that I’m creating a composite control which, by definition, is an grouping of child controls and that I had to EXPLICITLY tell it to ensure they exist.

    Go go .Net.

  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: