New to the .Net Framework 3.5 SP 1 is the System.Web.Routing namespace. The classes in the routing namespace allow you to use urls that do not map to a web page.
For this example I created a new web application. To start off with lets add a reference to the system.web.routing and system.web.abstractions. Open up the web.config file and lets add the UrlRoutingModule to the httpmodules section
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpModules>
Next we need to add a WebFormRouteHandler class which will be our route handler.
Imports System.Web.Routing
Imports System.Web.Compilation
Public Class WebFormRouteHandler
Implements IRouteHandler
Private _Path As String
Public Property Path() As String
Get
Return _Path
End Get
Set(ByVal value As String)
_Path = value
End Set
End Property
Public Sub New(ByVal p As String)
Path = p
End Sub
Public Function GetHttpHandler(ByVal requestContext As System.Web.Routing.RequestContext) As System.Web.IHttpHandler Implements System.Web.Routing.IRouteHandler.GetHttpHandler
For Each value In requestContext.RouteData.Values
requestContext.HttpContext.Items(value.Key) = value.Value
Next
If Not String.IsNullOrEmpty(Path) Then
Return TryCast(BuildManager.CreateInstanceFromVirtualPath(Path, GetType(Page)), IHttpHandler)
Else
Return Nothing
End If
End Function
End Class
Finally we have to add global.asax so we can start the routing when the web site starts up
Imports System.Web.SessionState
Imports System.Web.Routing
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started.
Dim userHandler As New WebFormRouteHandler("~/user.aspx")
With RouteTable.Routes
' pattern of the url to match
.Add(New Route("user/{user}", userHandler))
End With
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application ends
End Sub
End Class
Finally we need 2 web pages
Default.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="RoutingTest._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>ASP.NET System.Web.Routing with WebForms sample</h1>
<div><a href="user/ken">User test</a></div>
</div>
</form>
</body>
</html>
user.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="User.aspx.vb" Inherits="RoutingTest.User" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Users</h1>
<div>
User:
<%=Context.Items("user")%></div>
<hr />
<div>
Change the user in the URL.</div>
<div>
The WebFormRouteHandler adds the matched values to the HTTPContext.Items collection
(exists for lifetime of request only).</div>
</div>
</form>
</body>
</html>
Hope this helps
A button, has to be draged on a webform, and this code has than to be pasted in a webform code behind
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Me.Button1.Text = "Send Mail"
Me.Button1.Attributes("onClick") = window.location='" & _
"mailto:somebody@windowsformsdatagridhelp.com?" & _
"subject=DataGridHelp demo&body=I hope this helps?';"
End If
End Sub
Astro Boy has made a live clock in JavaScript. Here is showed how this can be used in code behind from ASPNET. For this sample is only needed a long label on a webpage and than run
Private Sub Page_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
Dim str As String = "<script language=javascript >" & _
"src='http://www.zip.com.au/~astroboy/liveclock/liveclock.js '>" & _
"show_clock()</ script>"
Page.RegisterClientScriptBlock("LiveClock", str)
Label1.Text = _
"<script language='javascript'>new LiveClock(); </ script>"
End Sub
'To use it this way is of course not fair, I here it is done to make
'the sample easy to show.
'It is nicer to download the script from Astro Boys website and
'than put it in your own project (IIS file)
'While Astro boy gives as well samples
'how to use it to let it look nicer if you use the link bellow
'http://www.zipworld.com.au/~astroboy/liveclock/
This clock tip is mainly meant to show:
How to make an image from scratch;
How to use a client side timer;
How to use an extra page to show an image.
It’s not meant to use on Internet because therefore it’s probably far to slow
'''Form 1 Needs an imagebox on the page
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Image1.Height = New Unit(32)
Image1.Width = New Unit(200)
Image1.Visible = True
'Beneath is the Timer
Dim scriptString As String = "<script language=JavaScript>" & _
"setclock(); function setclock(){document.images.Image1.src = " & _
"'http://localhost/WebClock/WebForm2.aspx';setTimeout('setclock()',1000)}</script>"
Page.RegisterStartupScript("setclock", scriptString)
End Sub
'''Form2 needs only an empty page
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Response.Cache.SetExpires(DateTime.Now.AddTicks(500))
Dim myForeBrush As Brush = Brushes.Black
Dim myFont As New Font("Times New Roman", 8, FontStyle.Regular)
Dim textHeight As Single
Dim bm As New Bitmap(120, 20)
Dim g As Graphics = Graphics.FromImage(bm)
g.Clear(Color.White)
Dim textSize As SizeF = g.MeasureString(now.tostring, myFont)
g.DrawString(Now.ToString, myFont, myForeBrush, _
New RectangleF(0, 0, 120, 20))
Dim ms As New IO.MemoryStream
Dim arrImage() As Byte
bm.Save(ms, Imaging.ImageFormat.Bmp)
arrImage = ms.GetBuffer
Response.BinaryWrite(arrImage)
g.dispose
End Sub
ATLAS is Microsoft's version of Ajax. It is available for free from there atlas website. For this example download and install the lastest version of atlas. You will need 2 labels (lblNow and lblTimer) and Atlas timer control and Atlas update panel. lblNow text will be set on page load. The update panel will cause the page to only redraw the lblTimer on the timer's tick event
Pages HTML
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Atlas Timer Control</title>
</head>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"/>
<div>
<asp:Label ID="lblNow" runat="server" Text=""></asp:Label><br />
<atlas:UpdatePanel runat="server" ID="up1" Mode="Always">
<ContentTemplate>
<asp:Label ID="lblTimer" runat="server" Text=""></asp:Label></ContentTemplate>
</atlas:UpdatePanel>
<atlas:TimerControl ID="tmr1" runat="server" Interval="1000">
</atlas:TimerControl>
</div>
</form>
<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<references>
</references>
<components>
</components>
</page>
</script>
</body>
</html>
The code behind File
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblTimer.Text = Now.ToLongTimeString
lblNow.Text = Now.ToLongTimeString
End Sub
Protected Sub tmr1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr1.Tick
lblTimer.Text = Now.ToLongTimeString
End Sub
End Class