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
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
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
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:
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;
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);
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)
{
numClick = 0;
runningtotal = 0;
}
Here is .aspx web controls:
<asp:ImageButton ID="pic" runat="server" ImageAlign="Left" OnClick="Image_Click" />
<asp:Button ID="Resetter" runat="server" Text="Reset" Width="120px" OnClick="Reset_It"/>
What I have tried:
protected void Page_Load(EventArgs e)
{
Session["Variable"] = any variable;
}