I am trying to add controls to the Dialog control dynamically. I add the controls to the ContentPane.TemplateControl.Controls collection. Whenever I do this, the control ends up on the page hosting the dialog and not in the dialog.
Is there something I need to do to get it to show up in the dialog correctly?
-Daniel
The idea behind the "Ensure" pattern is that it can be called multiple times and only call the "Create" method once. So in this case, EnsureTemplates can be called 100 times, and only call CreateChildControls once. That's the simplified version. In this case though, the code is checking for an explicit condition as well, to be sure that controls haven't already been added to the controls collection. It's sort of a safegaurd against adding the same content multiple times. Unfortunately, this check is what prevents new content from being added through the designer, if you add your own controls manually as you were initially doing. Either way, I suggest you download the source - it will help tremendously in this case, and that's what it's there for.. to help. Let me know if you still can't find a solution for this.
-Tony
Thanks again for the quick response. I did what you said, and it did not work. :) It looks like EnsureTemplates is called several times before it actually ensures that the templates are there, which seems strange. I added some logic to only add my additional controls once the template controls are there, but this feels hacky since I would expect EnsureTemplates to actually ensure the templates are there. :) I don't have the source code to see what is going on. I will need to download it and set that up to try and figure out what is really going on.
Thanks again for the help though.
Ok, I found the problem. If you look at the source code for the WebDialogWindow, you'll see an EnsureTemplates method. This method checks to see if the ContentTemplate !=null && !ContentPane.HasControls
Since your code above is actually adding controls to the ContentPane, EnsureTemplates will fail this check.
The fix is simple. Instead of adding your controls in CreateChildControls, you should add your controls inside of EnsureTemplates. Just be sure to call base.EnsureTemplates BEFORE you add anything to the ContentPane.Controls. Also, since the EnsureXXX pattern means that the method may be called multiple times, you'll want to wrap your controls.add code in a check to be sure that you haven't already called this method. Here's the code I would use:
public override void EnsureTemplates(){ if(!this.ContentPane.HasControls()){ base.EnsureTemplates(); this.ContentPane.Controls.Add(...); }else base.EnsureTemplates();}
Thanks for the response. I am not doing any of those things you mentioned. My code is very simple:
public class SaveDialog : WebDialogWindow { private TextBox _box;
protected override void CreateChildControls() { base.CreateChildControls();
_box = new TextBox(); _box.ID = "foo"; _box.Text = "yah";
ContentPane.Controls.Add(_box); }
Here is the code in the .ascx
<cc1:SaveDialog ID="SaveDialog1" runat="server"> <ContentPane> <Template> <asp:Button ID="Button1" runat="server" Text="Button" /> </Template> </ContentPane></cc1:SaveDialog>
The button doesn't show up, only the textbox.
Daniel,
Be sure you're calling base.CreateChildControls as this is responsible for taking the template and instantiating the controls into the WebDialogWindow. Also, be sure not to manually call "CreateChildControls" from your code anywhere. You should instead use EnsureChildControls (unless you're inside of the CreateChildControls method override).