Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
Project ASP.NET Snippets
Purpose
The purpose of this project is to demonstrate how to use ASP.NET 3.5 visual basic functions that perform various tasks such as cleaning whitespace, using multi-dimensional arrays, sharing web.config keys between the server and client, and, handling cookies.

Filter Whitespace From a String:
This snippet will filter whitespace from a string using regular expressions and also drop leading and trailing whitespace.
Imports System.Text.RegularExpressions

Public Function wSpaceRemove(ByVal wInputString As String) As String
' Removes whitespace from wInputString to nothing if there are no characters or a maximum of one whitespace in between characters or words.
' Removes leading and trailing whitespace as well.
Dim wSpaceRemoved As Integer = 0
While wSpaceRemoved = 0
wInputString = Regex.Replace(wInputString, "\s\s", "")
If Not Regex.IsMatch(wInputString, "\s\s") Then : wSpaceRemoved = 1 : End If
End While
wInputString = wInputString.Trim()
Return wInputString
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim FirstName As String = " a b c "
FirstName = wSpaceRemove(FirstName)
End Sub


Get The Value of a Cookie:
Public Function cookieFetch(ByVal cname As String) As String
Dim cookieName As String = String.Empty, cvalue As String = String.Empty
Dim cookieNameValues As New HttpCookieCollection
cookieNameValues = Request.Cookies
For Each cookieName In cookieNameValues
If LCase(cookieName) = LCase(cname) Then
cvalue = Request.Cookies(cookieName).Value
End If
Next
Return cvalue
End Function


Push Multi-Dimensional Array Into Listbox:
PAGE.ASPX
<asp:ListBox ID="selBox" Rows="1" runat="server"></asp:ListBox>
CODE-BEHIND (PAGE.ASPX.VB)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Create multi-dimensional array with values
Dim cardlist(2)() As String
cardlist(0) = New String() {"Select Option Value Text 1", "443"} ' use: "text", "value"
cardlist(1) = New String() {"Select Option Value Text 2", "444"}
cardlist(2) = New String() {"Select Option Value Text 3", "445"}
' Put multi-dimensional array into listbox
For y As Integer = 0 To UBound(cardlist)
selBox.Items.Add(New ListItem(cardlist(y)(0), cardlist(y)(1)))
Next
' Let's pre-select an option in the select box
Dim selectedIndex As Integer = 1
selBox.SelectedIndex = selectedIndex
End Sub


Share Web.Config Keys Between Server and Client:
NOTE: Define certain web.config keys as javascript variables (so the variables are easily shared between javascript running on the client, and use of the same variables by the server). In this example, "tojs_currentStep" is available to the server as something such as Response.Write(AppSettings("tojs_currentStep")). A custom javascript running on the client would reference the key as something such as alert(currentStep).
WEB.CONFIG
<?xml version="1.0"?>
<configuration>
<appSettings>
<!--
DECLARE VARIABLES HERE THAT WE WANT TO SHARE BETWEEN THE CLIENT AND THE SERVER. PREFIX WITH "tojs_".
-->
<add key="tojs_currentStep" value="7"/>
</appSettings>
<configuration>
PAGE.ASPX
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<%Response.Write(syncrhonizeJSParametersWithWebConfig())%>
</head>
<body>
...
</body>
</html>
CODE-BEHIND (PAGE.ASPX.VB)
Imports System.Configuration.ConfigurationManager

Public Function syncrhonizeJSParametersWithWebConfig() As String
Dim jsConfigure As String = "<script language=""text/javascript"" type=""text/javascript"">" & vbCrLf & "<!--" & vbCrLf & "/* Written by: Joe McCormack. This Javascript Block Genereated by the .Net Custom Function syncrhonizeJSParametersWithWebConfig() */" & vbCrLf
Dim key As String = String.Empty, keyName As String = String.Empty, keyValue As String = String.Empty
For Each key In AppSettings.AllKeys
keyName = key : keyValue = System.Configuration.ConfigurationManager.AppSettings(key)
If InStr(LCase(keyName), "tojs_") - 1 > -1 Then
If IsNumeric(keyValue) Then
jsConfigure = jsConfigure & "var " & Replace(keyName, "tojs_", "") & " = " & keyValue & ";" & vbCrLf
Else
keyValue = Replace(keyValue, "<", "<")
keyValue = Replace(keyValue, ">", ">")
keyValue = Replace(keyValue, """, """")
jsConfigure = jsConfigure & "var " & Replace(keyName, "tojs_", "") & " = " & """" & keyValue & """;" & vbCrLf
End If
End If
Next key
jsConfigure = jsConfigure & "//-->" & vbCrLf & "</script>" & vbCrLf
Return jsConfigure
End Function
About Joe