asp.net - Databinding on Custom Control with ITemplate -
this sample code of custom server control :
[designer(typeof(containercontroldesigner))] [toolboxdata("<{0}:blocarrondi runat=server><contenuprincipal></contenuprincipal></{0}:blocarrondi>")] public class blocarrondi : webcontrol { private itemplate _contenuprincipal; protected panel _panelcontenuprincipal = new panel(); public blocarrondi() : base(htmltextwritertag.div) { } [persistencemode(persistencemode.innerproperty)] [templateinstance(templateinstance.single)] public itemplate contenuprincipal { { return _contenuprincipal; } set { _contenuprincipal = value; } } protected override void oninit(eventargs e) { base.oninit(e); _panelcontenuprincipal.id = "panelprincipal"; this.controls.add(_panelcontenuprincipal); if (_contenuprincipal != null) _contenuprincipal.instantiatein(_panelcontenuprincipal); } }
and here implementation :
<controls:blocarrondi runat="server"> <contenuprincipal> <asp:label id="labelinfo" runat="server" /> </contenuprincipal> </controls:blocarrondi>
my label labelinfo accessible on code behind, great !
but if use custom control in repeater or listview, can't use container.dataitem property inside contenuprincipal template :
<asp:repeater id="repeaterinfos" runat="server"> <itemtemplate> <controls:blocarrondi runat="server"> <contenuprincipal> <asp:label runat="server" text="<%# (container.dataitem msginfo).displaymessage() " /> </contenuprincipal> </controls:blocarrondi> </itemtemplate> </asp:repeater>
the error message :
'system.web.ui.control' not contain definition 'dataitem' , no extension method 'dataitem' accepting first argument of type 'system.web.ui.control' found (are missing using directive or assembly reference?)
how can use container.dataitem property inside contenuprincipal template of control ?
i solved problem custom templatecontainer inherit idataitemcontainer :
public class blocarrondicontainer : panel, idataitemcontainer
change :
protected panel _panelcontenuprincipal = new panel(); [persistencemode(persistencemode.innerproperty)] [templateinstance(templateinstance.single)] public itemplate contenuprincipal { { return _contenuprincipal; } set { _contenuprincipal = value; } }
by :
protected blocarrondicontainer _panelcontenuprincipal = new blocarrondicontainer (); [persistencemode(persistencemode.innerproperty)] [templateinstance(templateinstance.single)] [templatecontainer(typeof(blocarrondicontainer))] public itemplate contenuprincipal { { return _contenuprincipal; } set { _contenuprincipal = value; } }
overrinding ondatabinding method on blocarrondi class :
protected override void ondatabinding(system.eventargs e) { if (namingcontainer idataitemcontainer) _panelcontenuprincipal.dataitem = (namingcontainer idataitemcontainer).dataitem; base.ondatabinding(e); }
Comments
Post a Comment