Saturday, March 24, 2012

UpdatePanel and CompositeControl

Hi,

I want to make a custom date selector server control. I am inheriting from the CompositeControl base class. As usual I override the "CreateChildControls" method and add my controls to the "Controls" collections. Among them is an UpdatePanel that should contain a Calendar control. I'm having problems creating and adding the UpdatePanel. How should I go about doing this?

This is my code so far for the "CreateChildControls" method:

namespace

Test.Presentation.Web {
publicclassDateTimeDropDown :CompositeControl {
UpdatePanel _updatePanel;
Calendar _calender;
testTemplate _template;
protectedoverridevoid CreateChildControls() {
_calender =newCalendar();
_updatePanel =newUpdatePanel();
_template =newtestTemplate(_calender);
_updatePanel.ContentTemplate = _template;
_updatePanel.ID =this.UniqueID +"_up";
this.Controls.Add(_updatePanel);
}
}
classtestTemplate :TemplateControl,ITemplate {
Control _control;
public testTemplate(Control control) {
_control = control;
}
publicvoid InstantiateIn(Control container) {
container.Controls.Add(_control);
}
}
}

I get the error message:System.InvalidOperationException: The UpdatePanel 'Test1_up' was not present when the page's InitComplete event was raised. This is usually caused when an UpdatePanel is placed inside a template.

I'm quite new to makeing custom controls so I'm probably missing something obvious here. Any suggestions to how I can accomplish this?

Thanks!

Ole Gulbrandsen

Pherhaps it is not possible to include an update panel in a composite control?

hello.

well, you can if you instantiate it before the init event (as the previous error reported)


Hi, thanks. This code works:

public class DateTimeDropDown : CompositeControl {
UpdatePanel _updatePanel;
Calendar _calender;
protected override void OnInit(EventArgs e) {

_updatePanel = new UpdatePanel();
_updatePanel.Mode = UpdatePanelMode.Always;
_updatePanel.ContentTemplate = new testTemplate();
_updatePanel.ID = "updt";

_calender = new Calendar();
_calender.ID = "cal";
_updatePanel.Controls.Add(_calender);
this.Controls.Add(_updatePanel);
base.OnInit(e);

}
}
class testTemplate : TemplateControl, ITemplate {
public void InstantiateIn(Control container) {
}
}

The above code, work, but if I build composite control of this one again I get into problems. I need to understand more of whats going on. Does anyone know any good sources for reading about how updatePanels fit into the page life cycle and how to incorporate them in custom controls ?

Ole


hello.

hum...i guess that there's not much to say:

* you have to add the trieggers till the end of the init event
* the panel initializes its template during the oninit event

i think this is all :)

you asked for resources...well, i guess that currently you only have .net reflector ;)


ok, I'll experiement a bit more with this, hope Microsoft can provide more documentation soon.

thanks for the help!

No comments:

Post a Comment