Create a simple template control. Having problems

I am trying to create a really simple template control. I have never done this before, but I know that many of my controls that I created in the past would help a lot if I enabled the templating feature - so I'm learning now.

The problem is that my template is displayed on the page, but my property value is missing. So all I get is static text, which I include in my template.

I have to do something right, because the control does not cause any errors, so it knows that my public property exists. (for example, if I try to use Container.ThisDoesntExist, it throws an exception).

I would be grateful for the help. I can be just a muppet and something is missing. Online tutorials on simple server management patterns seem small and far apart, so if you know one, I would like to know about it.

The version of my code is cut below.

Thanks a lot James

Here is my code for the control:

[ParseChildren(true)] public class TemplatedControl : Control, INamingContainer { private TemplatedControlContainer theContainer; [TemplateContainer(typeof(TemplatedControlContainer)), PersistenceMode(PersistenceMode.InnerProperty)] public ITemplate ItemTemplate { get; set; } protected override void CreateChildControls() { Controls.Clear(); theContainer = new TemplatedControlContainer("Hello World"); this.ItemTemplate.InstantiateIn(theContainer); Controls.Add(theContainer); } } 

Here is my container code:

 [ToolboxItem(false)] public class TemplatedControlContainer : Control, INamingContainer { private string myString; public string MyString { get { return myString; } } internal TemplatedControlContainer(string mystr) { this.myString = mystr; } } 

Here is my mark:

 <my:TemplatedControl runat="server"> <ItemTemplate> <div style="background-color: Black; color: White;"> Text Here: <%# Container.MyString %> </div> </ItemTemplate> </my:TemplatedControl> 
+4
source share
1 answer

you must call the DataBind method for your control.

One possibility is to add a DataBind call to the CreateChildControls () method:

protected override void CreateChildControls () {Controls.Clear ();

  theContainer = new TemplatedControlContainer("Hello World"); this.ItemTemplate.InstantiateIn(theContainer); Controls.Add(theContainer); this.DataBind(); } 
+1
source

Source: https://habr.com/ru/post/1306347/


All Articles