Generate Random Names from an Array Using AJAX

Introduction : AJAX (Asynchronous JavaScript and XML) is a new web development technique use for the interactive website. AJAX help we can developed web application and retrieve small amount of data from web server. AJAX consist a different type of technology.

  • JavaScript
  • XML
  • Asynchronous Call to the server

Array : The ArrayList object is a collection of items containing a single data value. Items are added to the ArrayList with the Add() method. The capacity of an ArrayList is the number of elements the list can hold. As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling Trim to Size or by setting the Capacity property explicitly. Array are using for store similar data type grouping as a single unit. We can access Array element by its numeric index. The array index start at zero(0). The default value of numeric Array element are set to zero(0), and reference element are set to null.

Syntax :

string[] NAMES = new string[];

Step 1 : Open Visual Studio 2010.

  • Go to File->New->WebSite.
  • Select ASP.NET WebSite.

Step 2 : Go to Solution Explorer and right-click.

  • Select Add->New Item.
  • Select WebForm.
  • Default.aspx page open.

Step 3 : Now go to the Default.aspx page and drag a control from Toolbox.

  • Drag ScriptManger, UpdatePanel, UpdateProgress,Button,Label

Step 4 : Now go to Default.aspx page and write the below code.

Code :

<title> My ajax application </title>
body bgcolor="#ffcccc">
    <form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div style="background-color: #CCFFCC">
    <br />
    <br />
    <br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate><fieldset><legend style="background-color: #00FFFF">Panel with Random Names</legend><br />
<asp:Button ID="Button1" runat="server" Text="Generate Random Names"
        OnClick="Button1_Click" BorderColor="#660066" />
<br />
<asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="100" DynamicLayout="true">
<ProgressTemplate><img border="1" src= "media.gif"  src = "Water lilies.jpg"/></ProgressTemplate>
asp:Label ID="lblNames" runat="server" ForeColor="#FF0066"></asp:Label>

Step 5 : Go to Default.aspx page and click in Design option.

  • Click in Button and select property option.
  • Define the Text name.

Step 6 : Now go to default.aspx.cs page option and write the below code.

Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    private readonly string[] NAMES = new string[]
    private void FillListBoxRandom()
        lblNames.Text = "";
        List<String> names = new List<string>();
        int count = NAMES.Length;
        for (int i = 1; i < 4; i++)
            Random rnd = new Random();
            int number = rnd.Next(count);
            string selName = NAMES[number];
            if (!names.Contains(selName))

        foreach (string name in names)
            lblNames.Text += name + "<BR />";
    protected void Button1_Click(object sender, EventArgs e)

Step 7 : We add the following assembly reference, because we are using a string array to store names.

Assembly :

using System.Collections.Generic;
using System.Collections;

Step 8 : Now run the Web application by press F5.


Step 9 : We click on the Generate Random Names option.


Now again click on the Generate Random Name option.


Similar Articles