Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - AJAX - AJAX Generate Random Names from Array in VB
AJAX Generate Random Names from Array in VB


ASP.NET AJAX Tutorial

This tutorial will show how we can use AJAX to dynamically generate a list of random names without calling postback. VB version.

This tutorial was created with Microsoft's ASP.NET AJAX Extensions, which can be downloaded at this link

This tutorial makes use of the UpdatePanel control in Visual Studio's AJAX toolbox. It will show you how we can generate random names from a string array at the click of a button, without postback.

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

The first thing we need to do is start an ASP.NET AJAX-Enabled Web Site within Visual Studio .NET
Then we can add our UpdatePanel, button and label to the ASPX page:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate><fieldset><legend>Panel with Random Names</legend><br />

<asp:Button ID="Button1" runat="server" Text="Generate Random Names" OnClick="Button1_Click" />
<br />

<asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="100" DynamicLayout="true">
<ProgressTemplate><img border="0" src="media/loading.gif" /></ProgressTemplate>
</asp:UpdateProgress>

<asp:Label ID="lblNames" runat="server"></asp:Label>
</fieldset></ContentTemplate>
</asp:UpdatePanel>
</div>
</form>

We add the following assembly reference, because we are goingto use a string array to store names:

Imports System.Collections.Generic

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

Then we create the string array:

Private ReadOnly strNames() As String = {"Mark", "Tom", "Harry", "Sally", "Sandra", "Paul", "Anastasia", "David", "Alex", "Michael", "Tina", "Zachary", "Bob", "Elise"}

Then we create the method that will choose a list of names at random:

Private Sub FillListBoxRandom()
lblNames.Text = ""

Dim names As List(Of String) = New List(Of String)()

Dim count As Integer = strNames.Length

For i As Integer = 1 To 3
System.Threading.Thread.Sleep(100)
Dim rnd As New Random()
Dim number As Integer = rnd.Next(count)
Dim selName As String = strNames(number)
If (Not names.Contains(selName)) Then
names.Add(selName)
End If
Next i

For Each name As String In names
lblNames.Text += name & "<BR />"
Next name
End Sub

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

The code-behind will look something like this:

Imports System.Collections.Generic

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
FillListBoxRandom()
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Private Sub FillListBoxRandom()
lblNames.Text = ""

Dim names As List(Of String) = New List(Of String)()

Dim count As Integer = strNames.Length

For i As Integer = 1 To 3
System.Threading.Thread.Sleep(100)
Dim rnd As New Random()
Dim number As Integer = rnd.Next(count)
Dim selName As String = strNames(number)
If (Not names.Contains(selName)) Then
names.Add(selName)
End If
Next i

For Each name As String In names
lblNames.Text += name & "<BR />"
Next name
End Sub

Private ReadOnly strNames() As String = {"Mark", "Tom", "Harry", "Sally", "Sandra", "Paul", "Anastasia", "David", "Alex", "Michael", "Tina", "Zachary", "Bob", "Elise"}
End Class

Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks