HOW TO GET A RANDOM VALUE IN BUTTON CLICK
random.aspx
random.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="random.aspx.cs" Inherits="random" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TextBox ID="TextBox1" runat="server" ToolTip="RANDOM VALUE"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="Button" onclick="Button1_Click" />
</form>
</body>
</html>
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Text;
public partial class random : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
}
private int RandomNumber(int
min, int max)
{
Random
random = new Random();
return
random.Next(min, max);
}
private string RandomString(int
size, bool lowerCase)
{
StringBuilder
builder = new StringBuilder();
Random
random = new Random();
char
ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26
* random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return
builder.ToString().ToLower();
return
builder.ToString();
}
public string GetPassword()
{
StringBuilder
builder = new StringBuilder();
builder.Append(RandomString(4, true));
builder.Append(RandomNumber(1000,
9999));
builder.Append(RandomString(2, false));
return
builder.ToString();
}
protected void Button1_Click(object
sender, EventArgs e)
{
TextBox1.Text = GetPassword();
}
}
Download source code:randomnumber