I have created a random password generator function in asp.net (vb.net). How do i call this function through my asp.net button???
:cry:

Function RandomPW(ByVal myLength)
'These constant are the minimum and maximum length for random
'length passwords. Adjust these values to your needs.
Const minLength = 6
Const maxLength = 20

Dim X, Y, strPW

If myLength = 0 Then
Randomize()
myLength = Int((maxLength * Rnd()) + minLength)
End If


For X = 1 To myLength
'Randomize the type of this character
Y = Int((3 * Rnd()) + 1) '(1) Numeric, (2) Uppercase, (3) Lowercase

Select Case Y
Case 1
'Numeric character
Randomize()
strPW = strPW & Chr(Int((9 * Rnd()) + 48))
Case 2
'Uppercase character
Randomize()
strPW = strPW & Chr(Int((25 * Rnd()) + 65))
Case 3
'Lowercase character
Randomize()
strPW = strPW & Chr(Int((25 * Rnd()) + 97))

End Select
Next

RandomPW = strPW

End Function

Recommended Answers

All 6 Replies

are you trying to put it on the aspx page or in the codebehind?

make it a static function in a class (say Utilities) then in your click event in code behind just call Utilities.RandomPW(7). But you need to return the password you generated.

I have created a random password generator function in asp.net (vb.net)

No you copied it without understanding it again. There is no way that you could have thought up that code and coded it and then not know how to call a function.

codes behind page.

here ya go hope this helps

WebForm1.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="vbtest.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>WebForm1</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body MS_POSITIONING="GridLayout">
	<form runat=server id="Form1">
		<asp:Button id="RndPassword" style="Z-INDEX: 100; LEFT: 392px; POSITION: absolute; TOP: 112px"
			runat="server" Text="RandomPassword"></asp:Button>
		<asp:Label id="lblNewPassword" style="Z-INDEX: 104; LEFT: 392px; POSITION: absolute; TOP: 152px"
			runat="server" Width="160px"></asp:Label>
		<asp:TextBox id="TextBoxPasswordLength" style="Z-INDEX: 101; LEFT: 336px; POSITION: absolute; TOP: 112px"
			runat="server" Width="28px"></asp:TextBox>
		<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 216px; POSITION: absolute; TOP: 112px" runat="server"
			Width="91px">Password Length</asp:Label>
			</form>
	</body>
</HTML>

WebForm1.aspx

Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents RndPassword As System.Web.UI.WebControls.Button
    Protected WithEvents TextBoxPasswordLength As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblNewPassword As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

    Private Sub RndPassword_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RndPassword.Click

        'These constant are the minimum and maximum length for random
        'length passwords. Adjust these values to your needs.
        Const minLength = 6
        Const maxLength = 20
        Dim myLength

        Try
            myLength = Convert.ToInt32(TextBoxPasswordLength.Text())

        Catch ex As Exception
            myLength = 0
        End Try
        Dim X, Y, strPW

        If myLength = 0 Then
            Randomize()
            myLength = Int((maxLength * Rnd()) + minLength)
        End If


        For X = 1 To myLength
            'Randomize the type of this character
            Y = Int((3 * Rnd()) + 1) '(1) Numeric, (2) Uppercase, (3) Lowercase

            Select Case Y
                Case 1
                    'Numeric character
                    Randomize()
                    strPW = strPW & Chr(Int((9 * Rnd()) + 48))
                Case 2
                    'Uppercase character
                    Randomize()
                    strPW = strPW & Chr(Int((25 * Rnd()) + 65))
                Case 3
                    'Lowercase character
                    Randomize()
                    strPW = strPW & Chr(Int((25 * Rnd()) + 97))

            End Select
        Next

        lblNewPassword.Text() = strPW



    End Sub
End Class

i like to add a special caracters, can you tel me how ?
thanks

@golden_leo

I'm glad you got it helpful.
Please do not resurrect old threads. If you have any questions please ask. You are welcome to start your own threads.

Thread Closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.