future
future

Reputation: 23

Asp.net count user attempt

I am new to ASP.net It might be a simple question but I dont know how to do it. I have a asp.wizard control on my page where On "step 1" I have one textbox that accept a order number If user enter the order number in textbox and it match with the database it move user to wizard step 2. If user entered wrong value or kept it blank then validator controls warn to enter value or entered value is wrong. However my concern it that if user try to input value more then three time it should show the popup window and say the message "you have tried three times and you dont have a valid order number" I tried to increment my counter in serverValidate event function but its reseting the counter value to 0 on every postback of the page. I dont now how to do it as everytime my page postback after clicking next button o my wizard page my counter value reset to 0

Upvotes: 1

Views: 2761

Answers (4)

future
future

Reputation: 23

if (Wizard1.ActiveStepIndex == 0)
    {
        if ((HiddenField1.Value == "0")){
            HiddenField1.Value = "1";
    }
        else if ((HiddenField1.Value == "1"))
      {
          HiddenField1.Value = "2";
      }

      else if ((HiddenField1.Value == "2"))
      {
          HiddenField1.Value = "3";

          Response.Write("<script language='javascript'>alert('you tried 3 times')</script>");
          }



    Label21.Text = HiddenField1.Value;
    } 

Upvotes: 0

Icarus
Icarus

Reputation: 63962

<input type="hidden" id="hdn" runat="server" value="0" />

On code behind, after each failed attempt:

hdn.Value=""+(ConvertToInt32(hdn.Value)+1);
if(hdn.Value=="3")
// do something

Upvotes: 2

competent_tech
competent_tech

Reputation: 44941

Store your counter in a hidden text field. This way the value will survive postbacks. You can either increment the counter in javascript (when the user presses the button) or on the server.

Here is a rough framework:

Add a field to your page similar to the following:

    <input type="hidden" runat="server" id="txtCounter" />

In your code-behind:

if String.IsNullOrEmpty(txtCounter.Value) {
   txtCounter.Value = "1";
} else {
   int wCounter;
   wCounter = Convert.ToInt32(txtCounter.Value);
   if (wCounter >= 3) {
     Page.ClientScript.RegisterStartupScript(Page.GetType(), "Count Exceeded", "alert('The number of tries has been exceeded.');", True)

   } else {
     wCounter += 1;
     txtCounter.Value = wCounter.ToString();
   }
}

Upvotes: 0

Orn Kristjansson
Orn Kristjansson

Reputation: 3485

You can save it in a session variable that one would not reset.

Upvotes: 2

Related Questions