Hi friends, in this article I will explain about How to
generate Text number in ASP.NET using VB.NET/C#, How to Generate Random Numbers
in ASP.NET.
In previous articles I explained about How to disable right click on Web page using JavaScript.,interview questions on SEO (Search Engine Optimization),how to remove the sessions in ASP.NET,How to remove specific session in ASP.NET.
It is very important because in so many sites generate
random text or number and asking enter that random number while registering or upload
something or download something.
In some sites when we sign up for some site then will
generate password and send to mail id or password. I will explain how it will
be generated with one small example.
Take one webpage and take one label and button like below.
In ASP.NET:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="randomnum.aspx.vb" Inherits="randomnum"
%>
<!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>Aspdotnet-roja:Generate
Random Text</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3 style="color:red">Aspdotnet-roja:Generate
Random Text</h3>
<asp:Label style="color :Green" ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Button ID="generate" runat="server" Text="Generate" onclick="generate_Click" Height="40px" Width="120px"
/>
</div>
</form>
</body>
</html>
|
VB.NET.Net Code to
create/generate unique random text in ASP.NET:
Partial Class randomnum
Inherits
System.Web.UI.Page
Dim num As String
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles Me.Load
End Sub
Protected
Sub generate_Click(ByVal
sender As Object,
ByVal e As
EventArgs) Handles generate.Click
num = GenerateRNumber(5)
Label1.Text = "Your Password is: " & num
End Sub
Public Shared Function
GenerateRNumber(ByVal Length As Integer) As String
Dim
_allowedChars As String
= "ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789abcdefghijkmnopqrstuvwxyz"
Dim
rndNum As New
Random()
Dim
num As Char()
= New Char(Length
- 1) {}
Dim
allowedCharCount As Integer
= _allowedChars.Length
For i
As Integer =
0 To Length - 1
num(i) =
_allowedChars(Convert.ToInt32((_allowedChars.Length) * rndNum.NextDouble()))
Next
Return New String(num)
End Function
End Class
|
C#.NET.Net Code to
create/generate unique random text in ASP.NET:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class randomnum :
System.Web.UI.Page
{
string num;
protected
void Page_Load(object sender,
System.EventArgs e)
{
}
protected
void generate_Click(object sender, EventArgs
e)
{
num = GenerateRNumber(5);
Label1.Text = "Your Password is: " + num;
}
public static string
GenerateRNumber(int Length)
{
string
_allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789abcdefghijkmnopqrstuvwxyz";
Random rndNum = new Random();
char[]
num = new char[Length];
int allowedCharCount =
_allowedChars.Length;
for
(int i = 0; i <= Length - 1; i++) {
num[i] =
_allowedChars[Convert.ToInt32((_allowedChars.Length) * rndNum.NextDouble())];
}
return
new string(num);
}
Public
randomnum()
{
Load += Page_Load;
}
}
|
Save and run the web page it will as shown in the below
figure.
No comments:
Post a Comment