March 9, 2021
Hot Topics:

ASP.NET Secrets, Part 2

  • By Karl Moore
  • Send Email »
  • More Articles »


Welcome to the second part of ASP.NET Secrets!

I'm Karl Moore and in this article, we'll be exploring a bundle of further .NET tricks to really put the shine on your Web applications. Today, we'll figure out:

  • Three Steps to Changing Your Page Title in Code!
  • How to Send Mail in ASP.NET
  • The Secret to Uploading Files with Ease
  • How to Dynamically Create Images

Ready to get stuck into .NET code?

Three Steps to Changing Your Page Title in Code!

If I asked you to change the title of your Web form, you'd be forgiven for looking for a "title" or "text" property somewhere. The problem is, that mysterious property doesn't exist: Microsoft forgot to put it in. If you want to change your page title programmatically, you have to do it yourself. Here's how.

Firstly, switch to the HTML view on your Web form. Near the top of your page, you'll see the title tag, looking something like <title>WebForm1</title>. Replace this with <title runat="server" id="PageTitle"></title>. Here, you're creating a title tag that runs on the server and has an ID, meaning you can manipulate it in code.

Next, switch back to design mode; then, open up the code window behind your form. At the top of your page, under the Inherits line, add the following line of code. This declares the server title tag you've just added:

Protected PageTitle _
          As System.Web.UI.HtmlControls.HtmlGenericControl

Our third and final step involves changing the page text. Behind the Load event of your page, or in response to some similar event, set the InnerText property of the PageTitle tag to your new page title. Here's my sample code:

PageTitle.InnerText = "Welcome! - Last Updated 20/05/2003"

And that's it—one line of code and your page title has changed. Hey, presto!

How to Send Mail in ASP.NET

Sending mail from a Web page is perhaps one of the most commonly requested code snippets. And to make your life easier, here's a function that encapsulates all the functionality for you. Simply call it, passing in the appropriate arguments, and you're sorted:

Public Function SendMail(ByVal [To] As String, _
    ByVal From As String, ByVal Subject As String, _
    ByVal Body As String, ByVal IsHTML As Boolean, _
    Optional ByVal SmtpServer As String = "domain.com") As Boolean
    ' Sends a mail message using the specified details
    ' - returns a False if delivery fails
        Dim objMsg As New System.Web.Mail.MailMessage()
        SendMail = True
        With objMsg
            .To = [To]
            .From = From
            .Subject = Subject
            .Body = Body
            ' .BodyFormat specifies whether the Body is
            ' in plain text or HTML format
            .BodyFormat = IIf(IsHTML = True, _
                System.Web.Mail.MailFormat.Html, _
        End With
        System.Web.Mail.SmtpMail.SmtpServer = SmtpServer
        SendMail = False
    End Try
End Function

In our function here, we're setting the SMTP server to a default—domain.com, at present. You'll need to change this to the address of your host SMTP server for the code to work.

Page 1 of 3

This article was originally published on March 11, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date