簡體   English   中英

回發時帶有C#列表增量的ASP.net

[英]ASP.net with C# List increment on postback

我有一個奇怪的問題。 在我的程序中,用戶操作下拉框,然后按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();
            }

也許這些東西甚至與問題都不相關,有什么想法可尋嗎?

我在OnInit事件中有一些東西,顯然每次頁面重新加載時它們都在運行。 我仍然不確定為什么

傑克(Jake),花10分鍾,閱讀一下頁面生命周期(特別是事件部分)。 它將為您節省寶貴的時間。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM