Answered by:
Adding template to custom control in design-time

Question
-
User559377399 posted
Hi,
i create a custom control which can hold some templates, such as Header, Body Footer, etc.
<cc1:myControl> <BodyTemplate> </BodyTemplate> </cc1:myCnntrol>
i tried to use smarttags to allow the user to edit each of the templates, which worked pretty well. what i could not get working was to add a template if it has not been in the control. eg. add a header-template to the control above. i tried using the RootDesigner.AddControlToDocument() function of my controldesigner. but this function gives me an error, telling me the control i'm going to add the template (i used a new HtmlGenericControl("HeaderTemplate") as new Control) to is not valid. (i can't give the exact error message, cause i use a german version of visual studio 2008)i've been searching the web for nearly half a day now, but i did'nt find a working solution for my problem
Thanks so far.
Tom
Saturday, March 13, 2010 2:57 PM
Answers
-
User559377399 posted
ok - eventually i managed to find a way.
the code you want to be added to the control has to be "created" when handling the smarttag method or property-changes.
im my case the designer has a Enum EditMode which defines, what's edited right now. i added the same enum-member to my smarttag and add the new template code in the get-handler. to add some code, just assign a template to the member of your control. i used a custom written DefaultHeaderTemplate.
code looks like that:
// the controls designer public class myControlDesigner { ... public EditModes EditMode; ... } // smart-tag actions for the designer myControlActionList { ... public EditModes EditMode { get { return ((myControlDesigner)Component).EditMode; } set { TypeDescriptor.GetProperties(Component)["HeaderTemplate"].SetValue(Component,new DefaultHeaderTemplate()); ((myControlDesigner)Component).EditMode = value; } } ... }
You can also add a template from some plain asp-code. change the code as follows:
TypeDescriptor.GetProperties(Component)["HeaderTemplate"].SetValue(Component, ControlParser.ParseTemplate((IDesignerHost)(Component.Site.GetService(typeof(IDesignerHost))), "[inner-template code here]"))
the string you pass to the Control-Parser should not contain the tags for the template itselfs. only write the content of the template. this did not work out when an empty string is passed. but a string with a single space is a good workaround.
i hope this helps.
Tom
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 19, 2010 12:09 AM
All replies
-
User1270895379 posted
I believe I have the same problem -- I have not tried your attempted solution.
Tom H.
Thursday, March 18, 2010 1:47 PM -
User559377399 posted
ok - eventually i managed to find a way.
the code you want to be added to the control has to be "created" when handling the smarttag method or property-changes.
im my case the designer has a Enum EditMode which defines, what's edited right now. i added the same enum-member to my smarttag and add the new template code in the get-handler. to add some code, just assign a template to the member of your control. i used a custom written DefaultHeaderTemplate.
code looks like that:
// the controls designer public class myControlDesigner { ... public EditModes EditMode; ... } // smart-tag actions for the designer myControlActionList { ... public EditModes EditMode { get { return ((myControlDesigner)Component).EditMode; } set { TypeDescriptor.GetProperties(Component)["HeaderTemplate"].SetValue(Component,new DefaultHeaderTemplate()); ((myControlDesigner)Component).EditMode = value; } } ... }
You can also add a template from some plain asp-code. change the code as follows:
TypeDescriptor.GetProperties(Component)["HeaderTemplate"].SetValue(Component, ControlParser.ParseTemplate((IDesignerHost)(Component.Site.GetService(typeof(IDesignerHost))), "[inner-template code here]"))
the string you pass to the Control-Parser should not contain the tags for the template itselfs. only write the content of the template. this did not work out when an empty string is passed. but a string with a single space is a good workaround.
i hope this helps.
Tom
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 19, 2010 12:09 AM