0

我有一个奇怪的问题。在我的程序中,用户操作下拉框并按下 ADD 按钮以用所选项目填充列表框。最终,我使用 INSERT 命令将这些项目发送到 MYSQL 数据库。当用户点击添加按钮时,项目被添加到通用列表和列表框中,并且在回发时一切似乎都很好,我可以整天添加东西。但是,当我点击提交/保存按钮,将数据发送到服务器时,列表会在每次回发时一遍又一遍地将添加的数据复制到自身,无论是什么原因导致回发。我可以在 lstbox 中看到结果,并且可以看到 List.count 上升。

我的 page_load 部分中没有代码。我确实使用会话属性来保存列表数据。

public List<string> activityProp
    {
        get
        {
            if (HttpContext.Current.Session["codelist"] == null)
            {
                HttpContext.Current.Session["codelist"] = new List<string>();
            }
            return HttpContext.Current.Session["codelist"] as List<string>;
        }
        set
        {
            HttpContext.Current.Session["codelist"] = value;
    }

}

这是按钮的代码。sqlsetter() 方法只有一个通用的 SQL 命令结构和一个插入字符串。它仅从此按钮触发。

protected void cmdSave_Click(object sender, EventArgs e)
    {
        //this will use the 'test' class to do a rule check on the day
        test therules = new test();
        bool comp = therules.check(dayProp, timeProp, activityProp);

        if (comp == true)
        {
            lblComp.Text = "You're ok!";
            lblComp.ForeColor = Color.Green;
            lblError.Text = "";
            SQLdaysetter();
        }
        else
        {   
            string zeerror = therules.getError();
            lblComp.Text = "You're not ok!";
            lblError.Text = "at " + zeerror;
            lblComp.ForeColor = Color.Red;
            lblError.ForeColor = Color.Red;
        }

    }

SQL的东西:

using (MySqlConnection conn = new    MySqlConnection(ConfigurationManager.ConnectionStrings["theConnectionString"].ConnectionString))
            {
                conn.Open();
                using (MySqlCommand cmdIns = new MySqlCommand(sqlstate, conn))
                {
                    cmdIns.ExecuteNonQuery();
                    cmdIns.Dispose();

                }
                conn.Close();
            }

也许这些东西甚至与问题无关,有什么想法在哪里看?

4

1 回答 1

1

我在 OnInit 事件中有一些东西,显然每次重新加载页面时它们都会运行。我仍然不确定为什么

Jake,请花 10 分钟时间阅读页面生命周期(尤其是事件部分)。它将为您节省未来宝贵的时间。

http://msdn.microsoft.com/en-us/library/ms178472.aspx

于 2012-04-25T22:33:18.750 回答