用户单击asp.net Web应用程序图像按钮时更新计数器 [英] Update counter when user clicks an asp.net web app imagebutton

查看:74
本文介绍了用户单击asp.net Web应用程序图像按钮时更新计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Web窗体应用程序,用户可以在其中打开ImageButton控件上的图像.在定义方法之前,我将全局int变量"counter"设置为零.每次用户单击ImageButton控件时,计数器"都应该增加一个.与ImageButton关联的OnClick方法正在触发,但是我认为每次单击后都将重置计数器".我知道这是因为只有Image_Click中的if分支正在执行.如何确保每次点击都记住计数器"的更新值?

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?

这是ImageButton的.aspx代码:

Here is .aspx code for ImageButton:

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

这是Image_Click的C#代码:

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;
        }
    }
}

推荐答案

您必须将点击计数存储在Sesson或Viewstate中,因为确实在每次加载页面后都会将其重置.与应用程序不同,网站变量仅在页面执行期间存在. 下面是一个简单的示例,说明如何在PostBack上持久保存变量.

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.";
}

这篇关于用户单击asp.net Web应用程序图像按钮时更新计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆