Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an ASP.NET Website which allows a user to open an image in an ImageButton and click on it. Most int and double variables are global to easily pass between methods without too many parameters. The global int variables include the size of image, numClicks (counter for clicks), distance, and running distance. Global double variables include a zoom factor (set depending on image size).

The OnClick event for the ImageButton is handled by a C# method
C#
protected void Image_Click(object sender, ImageClickEventArgs e)
. The method calculates the "real" distance between a pair of clicks by dividing the pixel distance, and dividing it by a static DotsPerInch value and zoom factor. The method also executes an incrementation of the running distance every time a pair of clicks occurs. There is also a "Reset" Button handled by a C# method
C#
protected void Reset_It(object sender, EventArgs e)
which sets running distance and numClicks back to zero.

At first, I tried storing the variables in Session states to survive accross ASP.NET postback, but that quickly got out of hand as I have about 20 variables in total. I learned that I can avoid PostBack by using Ajax implementations. My question is, how do I use Ajax implementations with ASP.NET C# OnClick handler methods?
Here is definition of some variables
C#
private int pos1x, pos1y = 0;
private int pos2x, pos2y = 0;
private int numClick = 0;
private int distx, disty = 0;
private int redistx, redisty, newdist = 0;
private int runningtotal = 0;
private double zoom = 1.0;


Here is the Image_Click method:
C#
protected void pictureBox1_Click(object sender, MouseEventArgs e)
        {
                numClick++;

            if (numClick% 2 == 1)
            {
                pos1x = e.X;
                pos1y = e.Y;
                pt = new Point(pos1x,pos1y);
                rects.Add(new Rectangle(pt, new Size(20, 20)));
                pictureBox1.Invalidate();
            }
            else
            {
                pos2x = e.X; // mouseUp position
                pos2y = e.Y; //
                pt = new Point(pos2x,pos2y);
                rects.Add(new Rectangle(pt, new Size(10, 20)));
                pictureBox1.Invalidate();
                
                float distx = Math.Abs(pos2x - pos1x); // length of measurement
                float disty = Math.Abs(pos2y - pos1y);
                redistx = (int)(Math.Ceiling((float)(distx / (1 / zoom * Math.Floor(dpiX / 4.0)))));
                redisty = (int)(Math.Ceiling((float)(disty / (1 / zoom * Math.Floor(dpiY / 4.0)))));
                newdist = (int)(Math.Ceiling(Math.Sqrt((redistx * redistx + redisty * redisty))));
            }
       }

Here is Reset_It method:
protected void resetCount_Click(object sender, EventArgs e)// reset count
       {
           numClick = 0;
           runningtotal = 0;
       }

Here is .aspx web controls:
<asp:ImageButton ID="pic" runat="server" ImageAlign="Left" OnClick="Image_Click" />
HTML


<asp:Button ID="Resetter" runat="server" Text="Reset" Width="120px" OnClick="Reset_It"/>


What I have tried:

C#
protected void Page_Load(EventArgs e)
{
    Session["Variable"] = any variable;
}
Posted
Updated 4-Oct-18 17:40pm

1 solution

In a pure ASP.Net world, you would need to make use of UpdatePanel. The controls that need to do the postback need to be in it. Here is UpdatePanel Control Overview[^] from MSDN.

Following is not a good approach for ASP.Net Web forms application IMHO.

You can also opt to perform AJAX get/post through javascript/jquery. In that case, you can handle the client side click event, perform ajax call and update the UI as needed. If you are using jQuery, there is an ajax function available. You can take a look at that.
 
Share this answer
 
v2
Comments
Member 13396929 5-Oct-18 0:10am    
Can I execute equivalent functions to Image_Click and Reset_It in Javascript without doing any postback? The variables don't need to go in a database because they only apply to one particular page, so that might help
dan!sh 5-Oct-18 5:23am    
Yes. You can do that in client side click event handlers using javascript

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900