I have created a Template composite control. This control is same as login control, when you just drag the control on page it will show default layout means it will not show anything inside the control . If you want to customize the layout, you can
modify its layout by using layout Template which i have created inside control.
But in login control, when you click on convert to template it will show tabular layout with labels and Textbox inside it. I want to build such feature for my control.
I am creating a simple Templated Composite control. I am creating this control for Email which is having label, TextBox and Regular Expression for email inside it. And this is the code
[
AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal),
Designer(typeof(EmailDesigner), typeof(IDesigner)),
DefaultProperty("Title"),
ToolboxData(
"<{0}:Email runat=\"server\"> </{0}:Email>"),
]
public class Email : CompositeControl,INamingContainer
{
#region Template
private ITemplate layoutTemplate;
private EmailTemplateOwner ownerValue;
#endregion
#region Variables
protected string associatedTextBoxId;
#endregion
#region Control
protected TextBox emailTextBox;
#endregion
#region Property
[
Browsable(false)
]
public TextBox EmailTextBox
{
get
{
return this.emailTextBox;
}
set
{
this.emailTextBox = value;
}
}
[
Bindable(true),
Category("Data"),
DefaultValue(""),
Description("Text")
]
public string Text
{
get
{
if (this.EmailTextBox == null)
{
return string.Empty;
}
else
{
return this.EmailTextBox.Text;
}
}
set
{
this.EmailTextBox.Text = value;
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public EmailTemplateOwner Owner
{
get
{
return ownerValue;
}
}
[
Browsable(false),
PersistenceMode(PersistenceMode.InnerProperty),
DefaultValue(typeof(ITemplate), ""),
Description("Control Layout Template"),
TemplateContainer(typeof(Email))
]
public ITemplate LayoutTemplate
{
get
{
return layoutTemplate;
}
set
{
layoutTemplate = value;
}
}
#endregion
protected override void CreateChildControls()
{
Controls.Clear();
ownerValue = new EmailTemplateOwner();
ITemplate temp = layoutTemplate;
if (temp == null)
{
temp = new EmailDefaultTemplate();
}
temp.InstantiateIn(ownerValue);
this.Controls.Add(ownerValue);
foreach (Control ctrl in this.Controls)
{
TextBox EmailTextBox;
if (string.IsNullOrEmpty(this.AssociatedTextBoxId))
{
EmailTextBox = (TextBox)ctrl.FindControl("EmailTextBox");
}
else
{
EmailTextBox = (TextBox)ctrl.FindControl(this.AssociatedTextBoxId);
}
if (EmailTextBox != null)
{
this.EmailTextBox = EmailTextBox;
}
}
}
}
#region Template Owner Class
[
ToolboxItem(false)
]
public class EmailTemplateOwner : WebControl
{ }
#endregion
#region Default Template
sealed class EmailDefaultTemplate : ITemplate
{
void ITemplate.InstantiateIn(Control owner)
{
Label emailLabel = new Label();
emailLabel.ID = "emailLabel";
emailLabel.Text = "Email Id ";
TextBox EmailTextBox = new TextBox();
EmailTextBox.ID = "EmailTextBox";
RegularExpressionValidator EmailRegularExpressionValidator = new RegularExpressionValidator();
EmailRegularExpressionValidator.ID = "EmailRegularExpressionValidator";
EmailRegularExpressionValidator.ControlToValidate = "EmailTextBox";
EmailRegularExpressionValidator.ErrorMessage = "Invalid Email";
EmailRegularExpressionValidator.Display = ValidatorDisplay.Dynamic;
EmailRegularExpressionValidator.ValidationExpression = @"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$";
owner.Controls.Add(emailLabel);
owner.Controls.Add(EmailTextBox);
owner.Controls.Add(EmailRegularExpressionValidator);
}
}
#endregion
#region Designer Class
public class EmailDesigner : ControlDesigner
{
public override void Initialize(IComponent Component)
{
base.Initialize(Component);
SetViewFlags(ViewFlags.TemplateEditing, true);
}
public override string GetDesignTimeHtml()
{
//return "<span>This is design-time HTML</span>";
return CreatePlaceHolderDesignTimeHtml("Click here and use " +
"the task menu to edit the templates.");
}
public override TemplateGroupCollection TemplateGroups
{
get
{
//TemplateGroupCollection collection = new TemplateGroupCollection();
TemplateGroupCollection collection = base.TemplateGroups;
TemplateGroup group;
TemplateDefinition template;
Email control;
control = (Email)Component;
group = new TemplateGroup("Layout Template");
template = new TemplateDefinition(this, "Template", control.ControlStyle, "Template", true);
Label emailLabel = new Label();
group.AddTemplateDefinition(template);
collection.Add(group);
return collection;
}
}
}
#endregion
There’s nothing very magical about “Convert to Template” in ASP.NET’s built-in controls.
You just have to write a bunch of code that generates the content that you want for the ITemplate property and then let the designer serialize that into ASPX markup.
The Login control has a LoginDesigner class that does its design time work. On LoginDesigner there’s a private ConvertToTemplateChangeCallback method that does some work to take the current “state” of the control (with all its current property values and
settings) and turns it into a control tree, which then gets saved onto the Login control’s template property, which is then saved by the VS designer into the ASPX markup.
I have created ControlActionList class which inherits DesignerActionList class. And i am able to add DesignerActionMethodItem ("Convert To Template" control task). But i am not able to modify html markup when you click on DesignerActionMethod.
pawar.vikask...
Member
146 Points
67 Posts
Template Composite control- Convert To Template
Jan 15, 2013 05:10 AM|LINK
Hi,
I have created a Template composite control. This control is same as login control, when you just drag the control on page it will show default layout means it will not show anything inside the control . If you want to customize the layout, you can modify its layout by using layout Template which i have created inside control.
But in login control, when you click on convert to template it will show tabular layout with labels and Textbox inside it. I want to build such feature for my control.
So please help me to solve this .
Thanks in Advance.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Template Composite control- Convert To Template
Jan 17, 2013 12:10 AM|LINK
Hi,
What control have you got?
What codes have you written?
pawar.vikask...
Member
146 Points
67 Posts
Re: Template Composite control- Convert To Template
Jan 17, 2013 04:22 AM|LINK
I am creating a simple Templated Composite control. I am creating this control for Email which is having label, TextBox and Regular Expression for email inside it. And this is the code
[ AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), Designer(typeof(EmailDesigner), typeof(IDesigner)), DefaultProperty("Title"), ToolboxData( "<{0}:Email runat=\"server\"> </{0}:Email>"), ] public class Email : CompositeControl,INamingContainer { #region Template private ITemplate layoutTemplate; private EmailTemplateOwner ownerValue; #endregion #region Variables protected string associatedTextBoxId; #endregion #region Control protected TextBox emailTextBox; #endregion #region Property [ Browsable(false) ] public TextBox EmailTextBox { get { return this.emailTextBox; } set { this.emailTextBox = value; } } [ Bindable(true), Category("Data"), DefaultValue(""), Description("Text") ] public string Text { get { if (this.EmailTextBox == null) { return string.Empty; } else { return this.EmailTextBox.Text; } } set { this.EmailTextBox.Text = value; } } [ Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) ] public EmailTemplateOwner Owner { get { return ownerValue; } } [ Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(typeof(ITemplate), ""), Description("Control Layout Template"), TemplateContainer(typeof(Email)) ] public ITemplate LayoutTemplate { get { return layoutTemplate; } set { layoutTemplate = value; } } #endregion protected override void CreateChildControls() { Controls.Clear(); ownerValue = new EmailTemplateOwner(); ITemplate temp = layoutTemplate; if (temp == null) { temp = new EmailDefaultTemplate(); } temp.InstantiateIn(ownerValue); this.Controls.Add(ownerValue); foreach (Control ctrl in this.Controls) { TextBox EmailTextBox; if (string.IsNullOrEmpty(this.AssociatedTextBoxId)) { EmailTextBox = (TextBox)ctrl.FindControl("EmailTextBox"); } else { EmailTextBox = (TextBox)ctrl.FindControl(this.AssociatedTextBoxId); } if (EmailTextBox != null) { this.EmailTextBox = EmailTextBox; } } } } #region Template Owner Class [ ToolboxItem(false) ] public class EmailTemplateOwner : WebControl { } #endregion #region Default Template sealed class EmailDefaultTemplate : ITemplate { void ITemplate.InstantiateIn(Control owner) { Label emailLabel = new Label(); emailLabel.ID = "emailLabel"; emailLabel.Text = "Email Id "; TextBox EmailTextBox = new TextBox(); EmailTextBox.ID = "EmailTextBox"; RegularExpressionValidator EmailRegularExpressionValidator = new RegularExpressionValidator(); EmailRegularExpressionValidator.ID = "EmailRegularExpressionValidator"; EmailRegularExpressionValidator.ControlToValidate = "EmailTextBox"; EmailRegularExpressionValidator.ErrorMessage = "Invalid Email"; EmailRegularExpressionValidator.Display = ValidatorDisplay.Dynamic; EmailRegularExpressionValidator.ValidationExpression = @"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$"; owner.Controls.Add(emailLabel); owner.Controls.Add(EmailTextBox); owner.Controls.Add(EmailRegularExpressionValidator); } } #endregion #region Designer Class public class EmailDesigner : ControlDesigner { public override void Initialize(IComponent Component) { base.Initialize(Component); SetViewFlags(ViewFlags.TemplateEditing, true); } public override string GetDesignTimeHtml() { //return "<span>This is design-time HTML</span>"; return CreatePlaceHolderDesignTimeHtml("Click here and use " + "the task menu to edit the templates."); } public override TemplateGroupCollection TemplateGroups { get { //TemplateGroupCollection collection = new TemplateGroupCollection(); TemplateGroupCollection collection = base.TemplateGroups; TemplateGroup group; TemplateDefinition template; Email control; control = (Email)Component; group = new TemplateGroup("Layout Template"); template = new TemplateDefinition(this, "Template", control.ControlStyle, "Template", true); Label emailLabel = new Label(); group.AddTemplateDefinition(template); collection.Add(group); return collection; } } } #endregionDecker Dong ...
All-Star
118619 Points
18779 Posts
Re: Template Composite control- Convert To Template
Jan 17, 2013 04:26 AM|LINK
Hi,
Your codes need more analyze……So I'll contact a senior developer to work with you.
Reguards!
pawar.vikask...
Member
146 Points
67 Posts
Re: Template Composite control- Convert To Template
Jan 25, 2013 11:22 AM|LINK
Did you ever get chance to contact your senior developer?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Template Composite control- Convert To Template
Jan 26, 2013 12:46 AM|LINK
Yes, I've escalated your issue to them, and they will analyze your problem in line.
Parashuram
Member
98 Points
39 Posts
Re: Template Composite control- Convert To Template
Jan 29, 2013 12:35 AM|LINK
There’s nothing very magical about “Convert to Template” in ASP.NET’s built-in controls.
You just have to write a bunch of code that generates the content that you want for the ITemplate property and then let the designer serialize that into ASPX markup.
The Login control has a LoginDesigner class that does its design time work. On LoginDesigner there’s a private ConvertToTemplateChangeCallback method that does some work to take the current “state” of the control (with all its current property values and settings) and turns it into a control tree, which then gets saved onto the Login control’s template property, which is then saved by the VS designer into the ASPX markup.
More inforrmation:
the code would look like this
<div class="line number148 index147 alt1">privateboolConvertToTemplateChangeCallback(objectcontext) {try{</div> <div class="line number149 index148 alt2">IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));</div> <div class="line number150 index149 alt1">ConvertToTemplateHelper convertToTemplateHelper =newConvertToTemplateHelper(this, designerHost);</div> <div class="line number151 index150 alt2">ITemplate template = convertToTemplateHelper.ConvertToTemplate();</div> <div class="line number152 index151 alt1">TemplateDescriptor.SetValue(_login, template);</div> <div class="line number153 index152 alt2">returntrue;</div> <div class="line number154 index153 alt1">}</div> <div class="line number155 index154 alt2">catch(Exception e) {</div> <div class="line number156 index155 alt1">Debug.Fail(e.Message);</div> <div class="line number157 index156 alt2">returnfalse;</div> <div class="line number158 index157 alt1">}</div> <div class="line number159 index158 alt2">}</div> <div class="line number159 index158 alt2"> </div> <div class="line number159 index158 alt2">privatesealedclassConvertToTemplateHelper :<div class="line number311 index310 alt2">LoginDesignerUtil.GenericConvertToTemplateHelper<LOGIN, LoginDesigner> {</div> <div class="line number312 index311 alt1"></div> <div class="line number313 index312 alt2">// Controls that are persisted when converting to template</div> <div class="line number314 index313 alt1">privatestaticreadonlystring[] _persistedControlIDs =newstring[] {</div> <div class="line number315 index314 alt2">"UserName",</div> <div class="line number316 index315 alt1">"UserNameRequired",</div> <div class="line number317 index316 alt2">"Password",</div> <div class="line number318 index317 alt1">"PasswordRequired",</div> <div class="line number319 index318 alt2">"RememberMe",</div> <div class="line number320 index319 alt1">"LoginButton",</div> <div class="line number321 index320 alt2">"LoginImageButton",</div> <div class="line number322 index321 alt1">"LoginLinkButton",</div> <div class="line number323 index322 alt2">"FailureText",</div> <div class="line number324 index323 alt1">"CreateUserLink",</div> <div class="line number325 index324 alt2">"PasswordRecoveryLink",</div> <div class="line number326 index325 alt1">"HelpLink",</div> <div class="line number327 index326 alt2">};</div> </div>pawar.vikask...
Member
146 Points
67 Posts
Re: Template Composite control- Convert To Template
Jan 30, 2013 10:49 AM|LINK
Hi,
I have created ControlActionList class which inherits DesignerActionList class. And i am able to add DesignerActionMethodItem ("Convert To Template" control task). But i am not able to modify html markup when you click on DesignerActionMethod.