ASP 101 - Active Server Pages 101 - Web04
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
Migrating to ASP.NET
Getting Scripts to Run on a Schedule
The Top 10 ASP Links @ Microsoft.com

QUICK TIP:
Variable Name Prefixes
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


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.


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers