I’m trying to create a templated composite control that would work in a similar fashion as the “PasswordRecovery” control of ASP.Net.
By that, I mean that the user can define its own template but, by using pre-defined controls ID, it defines which field is, say the e-mail address, and which button is the one to send the e-mail.
I’ve tried to look at the documentation for templated web server controls, but I can’t find anything talking about adding a behavior to those controls.
Alternatively, is there a way to change the behavior of the PasswordRecovery completely? I would like to send an e-mail with a one-time URL to change the password instead of the common behavior of that control.
I answered a related question:
https://stackoverflow.com/a/11700540/1268570
But in this answer I will go deeper.
I will post a templated server control with design support and with custom behavior:
Container code
Server Control
The
public string Addressproperty is used as control INPUT, you can create all the input properties you need in order to execute your task.public ITemplate AddressTemplate { get; set; }This represents the template of your control. The name you give to this property will be the name used in the page’s markup as the name of your templatepublic TemplatedServerAddressContainer AddressContainerThis property is just for designer supportIn order to create correctly the child controls you need to override the following methods and properties:
Controls,DataBindandCreateChildControlsOverriding the
OnBubbleEvent, you will be able to react to specific events coming from the control.Designer support
ASPX markup
ASPX code behind
Notice how you can set control’s properties without problems in the correct event:
this.addressControl1.Address = "Super Cool";Notice how your control can handle custom events
this.Response.Write("From custom button" + DateTime.Now.ToString());And finally, to indicate to your control that you want to perform something, just create a button with the command name exposed by your control like this:
<asp:Button Text="Command bubbled" runat="server" CommandName="DoSomething" OnClick="Unnamed2_Click1" />optionally, your button can contain an event handler that will be handled prior to bubbling the event.I uploaded this code sample completely functional to my GitHub for reference