Formatting Dates

This sample shows the different ways we can format dates, using ASP.Net. Just pick a format type from the listbox, and the details about that particular formatting will appear on the right.

<%@ Page Language="VB" %>
<script language="VB" Runat="server">
Dim myDate as DateTime
Sub Page_Load(Source as Object, E as EventArgs)
myDate=DateTime.Now
if not Page.IsPostBack then
	lbDate.Items.Add(New ListItem("Short Date Format", "d"))
	lbDate.Items.Add(New ListItem("Long Date Format", "D"))
	lbDate.Items.Add(New ListItem("Short Time Format", "t"))
	lbDate.Items.Add(New ListItem("Long Time Format", "T"))
	lbDate.Items.Add(New ListItem("Full Date/Time Format (Short time)", "f"))
	lbDate.Items.Add(New ListItem("Full Date/Time Format (Long time)", "F"))
	lbDate.Items.Add(New ListItem("General Date/Time Format (Short time)", "g"))
	lbDate.Items.Add(New ListItem("General Date/Time Format (Long time)", "G"))
	lbDate.Items.Add(New ListItem("Month day format", "m"))
	lbDate.Items.Add(New ListItem("RFC 1123 format", "r"))
	lbDate.Items.Add(New ListItem("Sortable date/time format", "s"))
	lbDate.Items.Add(New ListItem("Universable sortable date/time format (short)", "u"))
	lbDate.Items.Add(New ListItem("Universable sortable date/time format (long)", "U"))
	lbDate.Items.Add(New ListItem("Year Month", "Y"))
end if
End Sub

Sub doDate(Source as Object, E as EventArgs)
if lbDate.SelectedItem.Value <>"None" then
	lblPattern.Text=lbDate.SelectedItem.Value
	lblType.text=lbDate.SelectedItem.Text
	lblOutput.text="String.Format(""{0:" & lbDate.SelectedItem.Value & "}"", Item)"
	lblExample.text=String.Format("{0:" & lbDate.SelectedItem.Value & "}", myDate)
End If
End Sub

</script>
<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 3.1">
	<title>Formatting Dates</title>
</head>
<body>
<form id="form1" Runat="server">
<table>
	<tr>
		<td align="Left" valign="Top" Rowspan="5">
			<i><font Color="#FF0000"><b>Select Formatting Type:</b></font></i><br>
			<asp:ListBox id="lbDate"
				AutoPostBack="True"
				OnSelectedIndexChanged="doDate"
				Runat="server"/></td>
	</tr>
	<tr>
		<td align="Right" valign="Top"><b>Format Pattern:</b> </td>
		<td align="Left" valign="Top"><asp:Label ID="lblPattern" Runat="server" /></td>
	</tr>
	<tr>
		<td align="Right" valign="Top"><b>Format Type:</b></td>
		<td align="Left" valign="Top"><asp:Label ID="lblType" Runat="server" /></td>
	</tr>
	<tr>
		<td align="Right" valign="Top"><b>Output: </b></td>
		<td align="Left" valign="Top"><asp:Label ID="lblOutput" Runat="server" /></td>
	</tr>
	<tr>
		<td align="Right" valign="Top"><b>Example: </b></td>
		<td align="Left" valign="Top"><asp:Label ID="lblExample" Runat="server" />
		</td>
	</tr>
</table>
</form>
</body>
</html>

Formatting Currency – More Simple Math

The main purpose of this sample is to show how to change the format of text into Currency format. To do this, we take a small part of what might be a shopping basket type application, and show how to multiply the static number/Price from a Label, by the number chosen in a DropDownList. Then, we format it as a Currency type.

<%@ Page Language="VB" %>
<script language="VB" Runat="server">
Dim sCur as Double
Sub Page_Load(Source as Object, E as EventArgs)
lblShirtPrice.text="$25.35"
lblShirtPrice.text=String.Format("{0:c}", lblShirtPrice.text)
End Sub

Sub doMath(Source as Object, E as EventArgs)
	Dim outPut as Double
	if ddl.SelectedItem.Text<>"" then outPut=ddl.SelectedItem.Text * lblShirtPrice.text
	text1.text= String.Format("{0:c}", outPut)
End Sub

</script>
<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 3.1">
	<title>Formattng Currency - Simple Math</title>
</head>
<body>
<form id="form1" Runat="server">
Shirt : <asp:Label ID="lblShirtPrice" Runat="server" /><br>
Total: <asp:TextBox ID="text1" Runat="Server"/>
<asp:DropDownList id="ddl"
	AutoPostBack="True"
	OnSelectedIndexChanged="doMath"
	Runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</html>

DataReader's 'Has Rows' Property in ASP.Net 1.1

With version 1.0 of ASP.Net, there was no way of checking to see if a DataReader returned any records, without actually reading it first. This was a minor hassel and a difficult point of understanding for many.

Thankfully, with version 1.1, the MS team added the ‘HasRows’ property to the DataReader. Now, you can check the ‘HasRows’ property, and if there are any rows, return the data, and if the DataReader is empty, provide the appropriate message.

This sample uses a slightly modified Northwind Database (MS Access 2000).

If you leave the textbox blank – - it will return all rows of the table – - enter a last name which is not in the provided list, you will get the message that it doesn’t exist, which is returned, via the ‘HasRows’ property.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
<script language="VB" Runat="server">
Dim MySQL as string
Sub GetRecords(Source as Object, E as EventArgs)
Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
	server.mappath("\data\northwind.mdb") & ";"
if txtLastName.text <>"" then
	MySQL="Select [NWEmployees].[FirstName], [NWEmployees].[LastName], " & _
		"[NWEmployees].[Title] from NWEmployees Where lastname=@LastName"
else
	MySQL="Select [NWEmployees].[FirstName], [NWEmployees].[LastName], " & _
		"[NWEmployees].[Title] from NWEmployees"
End If

Dim MyConn as New OleDBConnection(strConn)
Dim objDR as OleDBDataReader
Dim Cmd as New OLEDBCommand(MySQL, MyConn)
cmd.Parameters.Add(New OLEDBParameter("@LastName", txtLastName.text))
	Try
		MyConn.Open()
		objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
		If objDR.HasRows then
			myDataGrid.DataSource = objDR
			myDataGrid.DataBind()
			myDataGrid.Visible="True"
			lblMsg.text=""
		else
			lblMsg.text="Sorry - but there are no records with the " & _
				"last name of " & txtLastName.text
			myDataGrid.Visible="False"
		End If
	Catch ex as Exception
		lblMsg.text=ex.message
	Finally
		MyConn.Close()
	End Try
End Sub
</script>
<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 3.1">
	<title>DataReader''s ''Has Rows'' Property in ASP.Net 1.1</title>
</head>
<body>
<form id="form1" Runat="server">

<asp:TextBox id="txtLastName" Runat="server" />
<asp:Button id="Button1"
	Text="Get Records"
	onclick="GetRecords" Runat="server" /><br>
<i>Valid last names are Davolio, Buchanan, Fuller,
	Leverling, Peacock, King, Callahan, Dodsworth, Suyama</i>
<br>
<asp:Datagrid Runat="server"
	Id="myDataGrid"
	GridLines="Both"
	cellpadding="0"
	cellspacing="0"
	Headerstyle-BackColor="#8080FF"
	Headerstyle-Font-Name="Arial"
	Headerstyle-Font-Size="8"
	BackColor="#C8E1FB"
	Font-Name="Arial"
	Font-Size="8"
	BorderColor="Black">
</asp:DataGrid>
<p>
<asp:Label ID="lblMsg" ForeColor="Red" Font-Bold="True" Runat="server" />
</form>
</body>
</html>

Using @@Identity with MS Access 2000 and Above

One of the great things about newer versions of SQL Server is the ability to retrieve the ID of the last inserted record, using @@Identity. However, with earlier versions of Access (before Access 2000), this was not possible. The great thing is that, with Access 2000 and above, it IS possible.

This code sample shows how to do this, assigning the retrieved id # to a variable and using it globally in another routine. To see a sample of how to do this with earlier versions of MS Access, click here

This sample uses the Biblio database, converted to MS Access 2000, and changing the ‘Year Born’ field to ‘YearBorn’. As we all know, it’s always recommended NOT to put spaces in field or table names.
:)

%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
<script language="VB" Runat="server">
Dim lgID as Long
Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
	server.mappath("\data\biblio2.mdb") & ";"
Sub doInsert(Source as Object, E as EventArgs)
	Dim MySQL as string = "Insert into Authors (Author, YearBorn) Values (@Author, @YearBorn)"
	Dim MyConn as New OleDbConnection(strConn)
	Dim Cmd as New OleDbCommand(MySQL, MyConn)
	With Cmd.Parameters
		.Add(New OleDbParameter("@Author", frmAuthor.text))
		.Add(New OleDbParameter("@YearBorn", frmYearBorn.text))
	End With
	MyConn.Open()
	cmd.ExecuteNonQuery()
	mySQL=""
	mySQL = "SELECT @@Identity FROM Authors"
	cmd.Parameters.Clear()
	cmd.CommandText = mySQL
	lgID = CType(cmd.ExecuteScalar(), Long)
	response.Write ("Success! - insert completed - <br>")
	MyConn.Close
	BindData()
End Sub

Sub Page_Load(Source as Object, E as EventArgs)

End Sub
Sub BindData()
	Dim sql as string = "Select Top 10 * from Authors order by AU_ID desc"
	Dim MyConn as New OleDBConnection(strConn)
	Dim objDR as OleDBDataReader
	Dim Cmd as New OLEDBCommand(sql, MyConn)
	MyConn.Open()
	objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
	MyDataGrid.DataSource = objDR
	MyDataGrid.DataBind()
	MyConn.Close()
	if lgID >0 then response.Write ("id = " & lgID)
	myTable.Visible="False"
	ph1.visible="True"
End Sub
Sub MakeNew(Source as Object, E as EventArgs)
	myTable.Visible="True"
	ph1.Visible="False"
	frmAuthor.Text=""
	frmYearBorn.text=""
End Sub
</script>
<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 3.1">
	<title>Using Identity with MS Access 2000 and Above</title>
</head>
<body>
<form id="form1" Runat="server">
<table id="myTable" runat="server">
	<tr>
		<td align="right">Author</td>
		<td> <asp:textbox id="frmAuthor" Runat="server" /></td>
	</tr>
	<tr>
		<td align="right">YearBorn</td>
		<td> <asp:textbox id="frmYearBorn" Runat="server" /></td>
	</tr>
	<tr>
		<td align="right">
		<asp:button id="button1"
			onclick="doInsert"
			Text="Submit"
			Runat="server" /></td>
		<td></td>
	</tr>
</table>
<asp:placeholder ID="ph1" Visible="False" Runat="server">
	<asp:Datagrid Runat="server"
		Id="MyDataGrid"
		GridLines="Both"
		cellpadding="0"
		cellspacing="0"
		Headerstyle-BackColor="#8080FF"
		Headerstyle-Font-Name="Arial"
		Headerstyle-Font-Size="8"
		BackColor="#9CD2FA"
		Font-Name="Arial"
		Font-Size="8"

		BorderColor="Black">
	</asp:DataGrid>
	<asp:Button id="btnVis"
		Text="Insert Another"
		onclick="makeNew"
		Runat="server" />
</asp:placeholder>
</form>
</body>
</html>

Adding Multiple TextBoxes to a Page Dynamically

Sometimes, you might find a need to dynamically add multiple TextBoxes (asp.net server controls) to your page, the number of which would depend on user input. In this example, we not only show how to add a dynamic number of textboxes to the page, but how to add an HTML line break after each one.

<script language="VB" Runat="server">
Dim x as Integer
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
	For x=1 to 5
		ddl.items.add(x)
	Next x
	ddl.items.insert(0,"")
end if

End Sub

Sub doBoxes(Source as Object, E as EventArgs)
Dim iDDL as Integer=Cint(ddl.selectedItem.text)
	if iDDL>0 then
		For x=1 to iDDL
			Dim objText as Object
			Dim objBR as Object
			objText = New System.Web.UI.WebControls.TextBox
			objBR= New System.Web.UI.HtmlControls.HtmlGenericControl
			objText.ID="textBox" & x
			objBR.InnerHTML="<br>"
			ph1.controls.Add(objText)
			ph1.controls.Add(objBR)
		next x
	End If
End Sub

</script>
<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 3.1">
	<title>Adding TextBoxes to Pages Dynamically</title>
</head>
<body>
<form id="form1" Runat="server">
	<asp:DropDownList id="ddl" Runat="server">
	</asp:DropDownList>
	<asp:Button id="btn" Text="Create Text Boxes"
		onclick="doBoxes" Runat="server" />
	<hr>
	<asp:placeholder ID="ph1" Runat="server">
	</asp:placeholder>
</form>
</body>

Using Stored Queries in MS Access

This sample shows how to use an OleDbCommand, in conjunction with an MS Access Stored Query/View to populate a DataGrid. Here, we use the Northwind database and the ‘Category Sales for 1995′ query that comes with the database.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
<script language="VB" Runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
	BindData
End Sub

Sub BindData()
	Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
	server.mappath("data\Northwind.mdb") & ";"
	Dim MyConn as New OleDBConnection(strConn)
	Dim ds as New DataSet
	Dim Cmd as New OleDBCommand()
	With Cmd
		.Connection=MyConn
		.CommandType=CommandType.StoredProcedure
		.CommandText = "[Category Sales for 1995]"
	end with
	Try
		MyConn.Open()
		mydatagrid.DataSource = Cmd.ExecuteReader
		mydatagrid.DataBind()
	Catch ex as Exception
		response.Write ("ex = " & ex.message)
	Finally
		MyConn.Close()
	End Try
End Sub
</script>
<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 3.1">
	<title>Using Stored Queries in MS Access</title>
</head>
<body>
	<form id="form1" Runat="server">
		<asp:Datagrid Runat="server"
			Id="MyDataGrid"
			GridLines="Both"
			cellpadding="0"
			cellspacing="0"
			Headerstyle-BackColor="#8080FF"
			Headerstyle-Font-Name="Arial"
			Headerstyle-Font-Size="12"
			BackColor="#C8E1FB"
			Font-Name="Arial"
			Font-Size="10"
			BorderColor="Black">
		</asp:DataGrid>
	</form>
</body>
</html>

Parsing a Path String with System.IO

With the ‘Substring’ property for string objects, parsing file names, from paths, seems at first, to be at least a little easier than it was with Classic ASP. However, you can also use the System.IO Namespace to parse your filenames for you, instead of using String.Substring. There are quite a few Properties you can use, however, I’m only going to show what I consider to be the most useful properties, when it comes to a substitution for manual parsing.

First, of course, we need to import the System.IO Namespace:
<%@ Import Namespace=”System.IO” %>

Then, to get the :
Root Drive – use
  Path.GetPathRoot(Path)
Directory Name – use
  Path.GetDirectoryName(Path)
FileName – use
  Path.GetFileName(Path)
FileName w/o Ext – use
  Path.GetFileNameWithoutExtension(Path)
Just the extension – use

  Path.GetExtension(Path)

Naturally, as I said earlier – there are other properties you can use, but this is just a sampling to show how parsing can be made a little easier.

<%@ Import Namespace="System.IO" %>
<script language="VB" Runat="server">
Dim sPath as String
Sub Page_Load(Source as Object, E as EventArgs)

End Sub
Sub ParseThePath(Source as Object, E as EventArgs)
Select Case Source.Id
	Case "LBParseThePath"
		label1.text="<b>The Directory of your file is</b> <font Color=''#FF0000''>" & _
		 Path.GetDirectoryName(PathName.Text) & "</font>"
	Case "LBBaseDrive"
		label1.text="<b>Root Drive for your file is</b>  <font Color=''#FF0000''>" & _
		Path.GetPathRoot(PathName.Text) & "</font>"
	Case "LBFileName"
		label1.text="<b>Your File Name (w/o Ext) is</b> <font Color=''#FF0000''>" & _
		Path.GetFileNameWithoutExtension(PathName.Text) & "</font>"
	Case "LBFileExt"
		label1.text="<b>Your Extension is</b> <font Color=''#FF0000''>" & _
		Path.GetExtension(PathName.Text) & "</font>"
	Case "LBFile"
		label1.text="<b>Your Full File Name is</b>  <font Color=''#FF0000''>" & _
		Path.GetFileName(PathName.Text) & "</font>"
End Select
End Sub

</script>
<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 3.1">
	<title>Parsing a Path String with System.IO</title>
</head>
<body>
<form id="form1" Runat="server">
<b><font Size="5" Color="#0000FF">Parsing a Path String with System.IO</font></b><p>
<table  cellpadding="2" cellspacing="2" border="0">
	<tr>
		<td align="left" valign="top">
		<asp:TextBox id="PathName"
			Text="c:\inetpub\wwwroot\MyFile.asp"
			 BorderStyle="Solid"
			 BorderWidth="1"
			 BorderColor="Gray"
			 Width="200"
			runat="server"></asp:TextBox></td>
		<td align="left" valign="top">
			<asp:LinkButton id="LBParseThePath"
				onclick="ParseThePath"
				runat="server"
				Text="Get Directory"></asp:LinkButton><br>
			<asp:LinkButton id="LBBaseDrive"
				onclick="ParseThePath"
				runat="server"
				Text="Get Base Drive Letter">
				</asp:LinkButton><br>
			<asp:LinkButton id="LBFileName"
				onclick="ParseThePath"
				runat="server"
				Text="Get FileName">
				</asp:LinkButton><br>
			<asp:LinkButton id="LBFileExt"
				onclick="ParseThePath"
				runat="server"
				Text="Get Extension"></asp:LinkButton><br>

			<asp:LinkButton id="LBFile"
				onclick="ParseThePath"
				runat="server"
				Text="Get Full File Name"></asp:LinkButton><br>
		</td>
	</tr>
</table>
<hr>
<asp:Label ID="label1" Font-Name="Arial" Runat="server" />
</form>
</body>
</html>

Editing an MS Access Record with a Form

This is the same sample as Editing a DataBase with a Form, which uses SQL Server, except this is how you would do it with MS Access. The main difference here, besides the whole SQL Client vs OleDb Namespace deal, is that, the parameters need to be called in the same order they are in the SQL statement. For instance, in the UPDATE sub – - since the WHERE clause is at the end of the SQL statement, the parameter in the WHERE clause must be at the end of the parameter declarations.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 2.1">
	<title>Choosing & Updating a Record</title>
<script language="VB" runat="server">
Dim intWdth, intID as integer
Dim strFirst, strLast, strTitle as string
Sub Page_Load(Source as Object, E as EventArgs)
	if not Page.IsPostBack then
		BindData
	End If
End Sub

Sub BindData()
	Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("\Data\TestInsertDB2.mdb") & ";"
	Dim sql as string = "Select id, fname, lname from TestInsert"
	Dim conn as New OleDbConnection(strConn)
	Dim Cmd as New OleDbCommand(sql, conn)
	Dim objDR as OleDbDataReader
	conn.Open()
	objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
	list1.datasource=objDR
	list1.DataValueField="ID"
	list1.datatextfield="Lname"
	list1.databind
	list1.selectedindex=0
	conn.Close()

End Sub

Sub EditRecord(Source as Object, E as EventArgs)
	Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("\Data\TestInsertDB2.mdb") & ";"
	Dim sql as string = "Select ID, Fname, Lname from TestInsert Where ID = "  & list1.selecteditem.value
	Dim conn as New OleDbConnection(strConn)
	Dim objDR as OleDbDataReader
	Dim Cmd as New OleDbCommand(sql, conn)
	conn.Open()
	objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
	While objDR.Read()
	   intID=objDR("ID")
	   strFirst=objDR("Fname")
	   strLast=objDR("Lname")
	End While
	databind()
	label1.text=""
	label2.text=""
		conn.Close

End Sub

Sub doUpdate(Source as Object, E as EventArgs)
	Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("\Data\TestInsertDB2.mdb") & ";"
	Dim MySQL as string = "Update TestInsert Set Fname=@Fname, Lname=@Lname Where id=@ID"
	Dim Conn as New OleDbConnection(strConn)
	Dim Cmd as New OleDbCommand(MySQL, Conn)

	cmd.Parameters.Add(New OleDbParameter("@Fname", frmFirstName.text))
	cmd.Parameters.Add(New OleDbParameter("@Lname", frmLastName.text))
	cmd.Parameters.Add(New OleDbParameter("@ID", lblID.text))
	Conn.Open()
	cmd.ExecuteNonQuery
	label1.text = "It''s Done!"

	label1.text="Successfully updated -- - "

	list1.SelectedIndex=List1.Items.IndexOf(List1.Items.FindByValue(lblID.text))
	conn.Close
	BindData
End Sub
</script>
</head>
<body>
<form Name="form1" runat="server">
<table border="1" width="100%">
	<tr>
		<td align="center"width="145" valign="Top"><b><i>Choose from List:</i></b><br>
<asp:ListBox id="list1" Width="125px" height="150px" runat="server" /><br>
<asp:button id="button1" Text="Edit Record" onclick="EditRecord" runat="server" />
		</td>

		<td align="Left" valign="Top">
<b>ID :</b> <asp:Label id="lblID" Text="<%# intID %>" runat="server" /><br>
<b>FirstName : </b> <asp:textbox id="frmFirstName" Text="<%# strFirst %>" runat="server" /><br>
<b>LastName : </b><asp:textbox id="frmLastName" runat="server" Text="<%# strLast %>" /><br>
<br>
<br>
<asp:button id="button2" Text="Update" onclick="doupdate" runat="server" />
<i><asp:Label ID="label1" forecolor="red" runat="server" /></i><br>
<i><asp:Label ID="label2" runat="server" /></i>
		</td>
	</tr>
</table>
</form>
</body>
</html>

Creating a Dynamic SQL statement from a CheckBoxList

In this example, we will show how to build a dynamic SQL statement, based on the items checked in a CheckBoxList. The CheckBoxList has the names of the Fields in the Database hardcoded here. (Getting them dynamically is a different Code Sample, yet to be uploaded.)

Basically, what we’re doing here is creating a string, based on the items checked in the list, and, using concatenation, we create the SQL statement. There are plenty of other Code Samples here which show how to display items from a database, so we won’t do that here. However, using this, along with the display code in other Samples, you will be well on your way to creating an ASP.Net page to use this method.

<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 3.1">
	<title>Creating a Dynamic SQL statement from a CheckBoxList</title>
<script language="VB" Runat="server">
Dim mySQL as String
Dim Stuff As String
Sub Page_Load(Source as Object, E as EventArgs)
	if not Page.IsPostBack then
		cbl.Items.add("PubID")
		cbl.Items.add("Name")
		cbl.Items.add("Company Name")
		cbl.Items.add("Address")
		cbl.Items.add("City")
		cbl.Items.add("State")
		cbl.Items.add("Zip")
		cbl.Items.add("Telephone")
		cbl.Items.add("Fax")
		cbl.Items.add("Comments")
	end if
End Sub
Sub ChooseFields(Source as Object, E as EventArgs)
	Dim Item As ListItem
	Dim Stuff as String
	For Each Item In cbl.Items
		If Item.Selected Then
			Stuff = Stuff & Item.Text & ", "
		End If
	Next
	If Stuff <> "" Then
		Stuff=Stuff.Substring(0,len(Stuff)-2)
		mySQL = "Select " & stuff & " from Publishers"
		label1.text=mySQL
	Else
		Label1.Text = "<b>Hey - Nothing was chosen!</b> "
		exit sub
	End If
End Sub

</script>
</head>
<body>
<form id="form1" Runat="server">
	<asp:CheckBoxList id="cbl"
		RepeatColumns="3"
		RepeatDirection="Vertical"
		Runat="server" />
	<asp:Button id="button1"
		Text="Choose Fields"
		onclick="chooseFields"
		Runat="server" />
	<p>
	<asp:Label ID="label1" Runat="server" />
</form>
</body>
</html>

Adding and Using an 'Other' Selection in a DropDownList

How many times have you needed an ‘Other’ selection in your DropDownList (or Select List)? When you need to give the end-user an alternate choice, then, you will need to provide a way for showing a textbox and then retrieving EITHER the text data or the Item chosen from the DropDownList. This is a highly simplified sample, but it should give you the basics of what you can then use.

<html>
<head>
<script language="VB" Runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
	if not Page.IsPostBack then
		LoadDDL
	end if
End Sub

Sub LoadDDL()
	ddl.Items.Clear
	ddl.Items.Add("")
	ddl.Items.Add("Red")
	ddl.Items.Add("Green")
	ddl.Items.Add("Blue")
	ddl.Items.Add("Other")
End Sub

Sub doChoice(Source as Object, E as EventArgs)
	if ddl.selecteditem.text="Other" then
		label1.visible="true"
		txtOther.visible="true"
	else
		label1.visible="false"
		txtOther.visible="false"
	End If
End Sub

Sub GetChoice(Source as Object, E as EventArgs)
	if txtOther.visible="true" and txtOther.text <>"" then
		lblResults.text="You chose " & txtOther.text & _
			" -- from the text box"
	else
		lblResults.text="You chose " & _
		ddl.selectedItem.text & " - - from the DropDownList"
	End If
	LoadDDL
	txtOther.text=""
End Sub
</script>
	<meta name="GENERATOR" Content="ASP Express 3.1">
	<title>Adding and Using an ''Other'' Selection in a DropDownList</title>
</head>
<body>
<form id="form1" Runat="server">
<asp:DropDownList id="ddl"
	AutoPostBack="True"
	OnSelectedIndexChanged="doChoice"
		Runat="Server">
	</asp:DropDownList>
<asp:Label ID="label1" Visible="False"
	Text="Other: " Runat="server" />
<asp:TextBox id="txtOther"
	Visible="False"
	Runat="server" />
<asp:Button id="button1"
	Text="Get Choice"
	onclick="getChoice" Runat="server" />
<p>
<asp:Label ID="lblResults" Runat="server" />
</form>
</body>
</html>