Thursday, March 29, 2012

Loop back to the text box

Hi all,
I'm trying to create a webpage that works as a password input with 3 strikes. Right now the webpage opens and there is a textbox with the cursor already in it, I can type the password and then press enter on the keyboard(works the same a clicking the box) and then it returns the result in a label. What I would like it to do is if an incorrect password is entered in the textbox, it is cleared out and the cursor appears again at the textbox however after 3 attempts it locks the user out or something to that affect.
Thankssince you already know how to set focus on thetextbox (using the javascript focus function I guess) so now in thepage_load event you can check that if he/she has already done 3 tries.This can be achieved using the ViewState variable. Let me know if youneed more help and I will be glad to help you.


this should work

PrivateSub Page_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

'Put user code to initialize the page here

SetFocus(Me.txtB)

IfNot IsPostBackThen

viewState("LOGIN_COUNTER") = 0

Else

viewstate("LOGIN_COUNTER") += 1

EndIf

EndSub

PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click

If viewState("LOGIN_COUNTER") = 3Then

Response.Write("last attempt")

Me.Button1.Enabled =False

Me.txtB.Enabled =False

Else

Me.txtB.Text = ""

EndIf

EndSub

'found this set focus code here http://ryanfarley.com/blog/archive/2004/12/21/1325.aspx

Sub SetFocus(ByVal ctrlAs Control)

Dim sbAsNew System.Text.StringBuilder("")

With sb

.Append("<script language='JavaScript'>")

.Append("function SetFocus()")

.Append("{")

.Append("document.")

.Append(ctrl.Parent.ID)

.Append("['")

.Append(ctrl.UniqueID)

.Append("'].focus();")

.Append("}")

.Append("window.onload = SetFocus;")

.Append("")

.Append("</script")

.Append(">")

EndWith

ctrl.Page.RegisterClientScriptBlock("SetFocus", sb.ToString())

EndSub
Hope this help
PQSIK


Assuming you got a TextBox control named TextBox1 and a Button Control named Button1 and a Label Control named Label1 this is how you would accomplish it.

privatevoid Page_Load(object sender, System.EventArgs e)

{

if (Session["LoginAttempts"] ==null)

Session["LoginAttempts"] = 0;

}

privatevoid Button1_Click(object sender, System.EventArgs e)

{

if ((int)Session["LoginAttempts"] < 3)

{

if (TextBox1.Text == "Secret")

{

Label1.Text = "Logged in";

}

else

{

Label1.Text = "Invalid Login Information";

TextBox1.Text =string.Empty;

Session["LoginAttempts"] = (int)Session["LoginAttempts"] + 1;

}

}

else

{

Label1.Text = "Sorry you have reached the maximum number of retries";

}

}

0 comments:

Post a Comment