本文介绍了在ASP.NET页面重载时为什么运行按钮事件的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class Stats : System.Web.UI.Page
{

    public SqlDataReader DataReader;
    public SqlCommand Command;
    string queryString = ("INSERT INTO UserData (UserProfileID, ConfidenceLevel, LoveLevel, HappinessLevel) VALUES ('a051fc1b-4f51-485b-a07d-0f378528974e', 1, 1, 1);");


    protected void Page_Load(object sender, EventArgs e)
    {
       LabelUserID.Text = Membership.GetUser().ProviderUserKey.ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        MySqlConnection database = new MySqlConnection();
        database.CreateConn();

        Command = new SqlCommand(queryString, database.Connection);
        Command.ExecuteNonQuery();        

    }

}

不知道我理解为什么的button1_Click()是在一个页面重新加载执行。结果
我把在功能一个破发点,它并击中它,以及更新数据库。

Not sure I understand why Button1_Click() is executing on a page reload.
I put in a break point in the function and it does hit it as well as update the database.

有什么建议?

感谢你。

推荐答案

如果您单击该按钮后回来,然后回传后重装,你基本上重复回传,这将导致按钮事件再次执行。如果你只是一个GET请求后重新加载页面不会出现这种情况。

If you click the button to post back, then reload after the postback, you're essentially repeating the postback, which will cause the button event to execute again. This will not happen if you simply reload the page after a GET request.

这篇关于在ASP.NET页面重载时为什么运行按钮事件的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:44