Monday, September 8, 2008

Handy ASP.NET Email Control

By Trevor Swanepoel

An email feedback control written in VB.NET

Most websites have the need for some sort of "feedback form" or "email-us" page, which, as we know, is tedious to re-develop on every site, even in .NET. I have developed this simple E-Mail control in VB.NET that can be wrapped around any form to be turned into an email form, even with file inputs.

The first major thing to overcome is the property which must be set just above the CLASS in the .ascx.vb file (no extra linebreaks). This allows the User Control to access the controls on the .aspx page where it is used.

_ 'No extra linebreak after this!!
Public MustInherit Class MailAnything
Inherits System.Web.UI.UserControl

If you do have a file input on your web form remember to set the enctype="multipart/form-data".

Another important thing to remember is to use Web Forms Labels to name your fields on the web form so that it can be read by your user control and form part of the email.

The basic elements of the form in the .aspx file is as follows (example).

Also don't forget to register your User control in the .aspx file

<%@ Register TagPrefix="Bluegrass" TagName="Mailer" Src="controls/MailAnything.ascx" %>
The rest is then all done in the user control which can be re-used in any project. Here is how I did the user control:


ASP.Net Feeds