Inheriting An ASP.NET Server Control

Using the Example

The following fragment illustrates how the PromptButton class is declared within an .aspx page:

<dc:PromptButton id="WithPrompt" runat="server"
Text="With Prompt" ConfirmMessage="Are you sure?" />

It is important to note that the string after the tag prefix gets combined with the associated namespace to form the fully qualified type name - LaMarvin.Samples.Web.DerivingBuiltinControls.PromptButton in this case.

That's all if you're adding your derived controls through the HTML view of the VS.NET WebForms designer. Funnily enough, when I modified the .aspx page in the HTML view by adding the derived PromptButton controls, the designer generated the corresponding declarations in the .aspx.vb code-behind file only when I've switched to the Design view at least once.

You can also add your derived control to the WebForms designer toolbox so you can create the control's instances using drag & drop from the toolbox. Just right-click the toolbox, choose ' Add/Remove Items...' and browse for the assembly .dll file (it's in the project's bin subdirectory). In order to better support the design-time experience, you might want to customize the initial HTML code that is generated by the designer when you place a new instance of your control onto a form.

You can do this by applying the System.Web.UI.ToolboxDataAttribute attribute to your derived control class:

<ToolboxData("<{0}:PromptButton runat=""server"" ConfirmMessage=""Hello!"" />")> _
Public Class PromptButton
Inherits System.Web.UI.WebControls.Button

Note that you must always include the string "{0}", which gets replaced be the actual tag prefix used within the page. Also you have to include the runat = "server" attribute, otherwise the designer won't be able to instantiate your control.

Beware, however, that when you change the ToolboxDataAttribute for a control that you've already put onto the toolbox, you'll have to remove it and re-add it again (recompile doesn't suffice). I don't know the reason for this. It seems that the designer is caching the attribute's value somewhere when the control is added to the toolbox.

Despite this small annoyance, deriving from an ASP.NET server control might be useful technique if you need functionality that "is-a" extension of an existing server control.

You might also like...

Comments

About the author

Palo Mraz

Palo Mraz United States

I live in Slovakia with my wife, two sons (fulltime), one daughter (occasionally) and a dog. I've been doing Microsoft Windows development since 1988; primarily in VB. I'm a big fan of the MS .N...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“You can stand on the shoulders of giants OR a big enough pile of dwarfs, works either way.”