Build Your Own ASP.NET Server Control Component
aka. <my:Component... Runat="Server" />
by alfred_ng@hotvoice.com
Here will deliver a simple tutorial guide you how to build a component
using VB.NET, and the steps using it with ASP.NET Web Page.
This component will generate the bullet or numbering beside your list.
Below is the coding of the component
Step1: Copy the code below save it as "ShowList.vb" filename.
================== Code Listing: ShowList.vb ========================
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Collections
Namespace listControl
Public Class ShowList: Inherits Control
Private _dataSource As IEnumerable
Public Property DataSource As IEnumerable
Get
Return _dataSource
End Get
Set
_dataSource = Value
End Set
End Property
Public Title As String
Public Format As String
Protected Overrides Sub OnDataBinding (e As EventArgs)
Dim DataEnum As IEnumerator
Dim ltlLiteral As LiteralControl
Dim lstBeginFormatTag As String
Dim lstEndFormatTag As String
Controls.Clear()
Controls.Add(New LiteralControl("<b>" & Title & "</b>"))
Select Case Format.ToLower()
Case "number"
lstBeginFormatTag = "<OL>"
lstEndFormatTag = "</OL>"
Case "bullet"
lstBeginFormatTag = "<UL>"
lstEndFormatTag = "</UL>"
End Select
If Format.ToLower() <> "number" AND Format.ToLower() <> "bullet" Then
Controls.Add(New LiteralControl("<br><font color='red'>" _
& "Error Message: Undefined ""Format"" Property, either " _
& """number"" or ""bullet""</font><br>"))
Else
If Not DataSource Is Nothing
DataEnum = DataSource.GetEnumerator()
Controls.Add(New LiteralControl(lstBeginFormatTag))
While (DataEnum.MoveNext())
ltlLiteral = New LiteralControl (DataEnum.Current.ToString())
Controls.Add(New LiteralControl("<LI>"))
Controls.Add(ltlLiteral)
Controls.Add(New LiteralControl("</LI>"))
End While
Controls.Add(New LiteralControl(lstEndFormatTag))
Else
Controls.Add(New LiteralControl("<br><font color='red'>" _
& "Data Not Found!</font><br>"))
End If
End If
End Sub
End Class
End Namespace
================== End of Code Listing ==============================
Step2: Compile and locate it at your web application root\bin folder.
vbc /t:library /r:System.dll,System.Web.dll ShowList.dll
Step3: Implement into your ASP.NET page, copy it and save it as
"ListControl.aspx".
================== Code Listing: ListControl.aspx ===================
<%@ Register TagPrefix="listControl" Namespace="listControl"
Assembly="ShowList"%>
<Script Runat="Server">
Sub Page_Load
Dim theList As ArrayList
theList = New ArrayList
theList.Add ("Expresso")
theList.Add ("Cappurino")
theList.Add ("Coffee")
theList.Add ("Mocha")
dbBound.DataSource = theList
dbBound.DataBind()
End Sub
</Script>
<html>
<head><title>ListControl Demonstration</title></head>
<body>
<h2>Build Your Own Bullet and Numbering Control Component</h2><br>
<listControl:ShowList id="dbBound" Title="Coffee Shop Menu"
Format="bullet" Runat="Server"/>
</body>
</html>
================== End of Code Listing ==============================
In order to use the control, remember that locate the "ShowList.dll" file
which is just generated
into the web application root\bin folder. Inside your .aspx page, you need
to attach this code
on top
<%@ Register TagPrefix="listControl" Namespace="listControl" Assembly="ShowList"%>
Inside the content, you need to attach the code below to using the control.
<listControl:ShowList id="dbBound" Title="Coffee Shop Menu" Format="bullet" Runat="Server"/>
Description: Initially the Page_Load will bind the data into the control. You may bind
the data with the database. For the "Format" property you can use either "bullet"
or "number" to display your list. The "Title" property,
it is just a title for the listing and in "BOLD" format.
Thanks for reading and try it out.
Any suggestion or recommendation about this control are welcome reply to me.