|
 |
This tutorial will show how we can use AJAX to dynamically generate a list of random names without calling postback. C# 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. 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 moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
We add the following assembly reference, because we are goingto use a string array to store names:
| using System.Collections.Generic; |
Then we create the string array:
private readonly string[] NAMES = new string[]
{
"Mark", "Tom", "Harry", "Sally", "Sandra", "Paul", "Anastasia", "David", "Alex", "Michael", "Tina", "Zachary", "Bob", "Elise" }; |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
Then we create the method that will choose a list of names at random:
private void FillListBoxRandom() {
lblNames.Text = ""; List<String> names = new List<string>(); int count = NAMES.Length; for (int i = 1; i < 4; i++) {
System.Threading.Thread.Sleep(100); Random rnd = new Random(); int number = rnd.Next(count); string selName = NAMES[number]; if (!names.Contains(selName)) {
names.Add(selName); } } foreach (string name in names)
lblNames.Text += name + "<BR />"; } |
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 code-behind will look something like this:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Web.UI.MobileControls; using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) {
FillListBoxRandom(); } private readonly string[] NAMES = new string[] {
"Mark", "Tom", "Harry", "Sally", "Sandra", "Paul", "Anastasia", "David", "Alex", "Michael", "Tina", "Zachary", "Bob", "Elise" }; private void FillListBoxRandom() {
lblNames.Text = ""; List<String> names = new List<string>(); int count = NAMES.Length; for (int i = 1; i < 4; i++) {
System.Threading.Thread.Sleep(100); Random rnd = new Random(); int number = rnd.Next(count); string selName = NAMES[number]; if (!names.Contains(selName)) {
names.Add(selName); } } foreach (string name in names)
lblNames.Text += name + "<BR />"; } } |
Looking for the VB.NET 2005 Version? Click Here! Looking for more ASP.NET Tutorials? Click Here!
|
|
|