0

I have an ASP.NET web forms application where the user opens up an image on an ImageButton control. I set a global int variable "counter" to zero outside before defining methods. Every time the user clicks on the ImageButton control the "counter" is supposed to increase by one. The OnClick method associated with the ImageButton is firing, but I think the "counter" is being reset after each click. I know this because only the if branch in Image_Click is being executed. How can I make sure that the updated value of "counter" is remembered for each click?

Here is .aspx code for ImageButton:

<asp:ImageButton ID="pic" runat="server" OnClick="Image_Click" />

Here is c# code for Image_Click:

public int numClick++;

protected void Image_Click(object sender, ImageClickEventArgs e)
{
    numClick++;

    if (numClick % 2 == 1)
    {
        pos1x = e.X;
        pos1y = e.Y;
        labelarea.Text = " " + pos1x;

    }
    else if (numClick % 2 == 0)
    {
        pos2x = e.X;
        pos2y = e.Y;
        distx = Math.Abs(pos2x - pos1x);
        disty = Math.Abs(pos2y - pos1y);
        redistx = (int)(Math.Ceiling((float)(distx / (zoom * Math.Floor(dpiX / 4.0)))));
        redisty = (int)(Math.Ceiling((float)(disty / (zoom * Math.Floor(dpiY / 4.0)))));
        if (mode == 1)
        {
            if (distx >= disty)
            {
                lengthlabel.Text = "Length: " + redistx;
                total += redistx;
            }
            else
            {
                lengthlabel.Text = "Length: " + redisty;
                total += redisty;
            }
            labeltotal.Text = "Total: " + total;
        }
    }
}
3
  • You are correct the server basically has no idea when you do the 2nd click. You gotta store the value in a database or on the page depending how important the information is. Commented Sep 28, 2018 at 18:45
  • I just need to store it on webpage. How do I go about it? Commented Sep 28, 2018 at 19:03
  • You can probably store it in a javascript variable or hidden field and increment that without posting back Commented Sep 28, 2018 at 22:40

1 Answer 1

1

You have to store the click count in a Sesson or Viewstate because it is indeed reset after each page load. Unlike apps, a website variables only exists during the life op the page execution. Below a simple example of how to persist a variable across PostBack.

protected void Image_Click(object sender, EventArgs e)
{
    //create a variable for the clicks
    int ButtonClicks = 0;

    //check if the viewstate exists
    if (ViewState["ButtonClicks"] != null)
    {
        //cast the viewstate back to an int
        ButtonClicks = (int)ViewState["ButtonClicks"];
    }

    //increment the clicks
    ButtonClicks++;

    //update the viewstate
    ViewState["ButtonClicks"] = ButtonClicks;

    //show results
    Label1.Text = "Button is clicked " + ButtonClicks + " times.";
}
Sign up to request clarification or add additional context in comments.

1 Comment

It does work for numClick but when i applied it to the int total, it didnt work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.