3

我在 page_load 上运行以下代码

 using (SqlConnection conn = new SqlConnection("myconnectionString"))
        {
            conn.Open();
            using (SqlCommand cmmnd = new SqlCommand("", conn))
            {



                cmmnd.CommandText = "SELECT * FROM addsetting;";


                SqlDataReader rdr = cmmnd.ExecuteReader();
                while (rdr.Read())
                {
                    count++;
                    param = Convert.ToString(rdr["rowno"]);
                    TextBox1.Text = Convert.ToString(rdr["tostudent"]);
                    TextBox2.Text = Convert.ToString(rdr["tofaculty"]);
                    TextBox3.Text = Convert.ToString(rdr["studentday"]);
                    TextBox4.Text = Convert.ToString(rdr["facultyday"]);
                    TextBox5.Text = Convert.ToString(rdr["firstweek"]);
                    TextBox6.Text = Convert.ToString(rdr["secondweek"]);
                    TextBox7.Text = Convert.ToString(rdr["thirdweek"]);

                }
                rdr.Close();
            }
            conn.Close();}

现在填写此表格后,我正在更改值并有一个“保存”按钮。

奇怪的是,在 Button2_Click 上,当我尝试从文本框中访问已编辑的值时,我没有得到新的值。文本框有旧值。

为什么文本框没有编辑过的值。

下面是我用来访问值的代码。

 using (SqlConnection conn = new SqlConnection("myconnectionstring"))
            {
                conn.Open();



                using (SqlCommand cmd = new SqlCommand("", conn))
                {
                    cmd.Parameters.Add("@rowno", SqlDbType.VarChar).Value = param; 

                    cmd.Parameters.AddWithValue("@tostudent", TextBox1.Text); 
                    cmd.Parameters.AddWithValue("@tofaculty", TextBox2.Text);
                    cmd.Parameters.AddWithValue("@studentday", TextBox3.Text);
                    cmd.Parameters.AddWithValue("@facultyday", TextBox4.Text);
                    cmd.Parameters.AddWithValue("@firstweek", TextBox5.Text);
                    cmd.Parameters.AddWithValue("@secondweek", TextBox6.Text);
                    cmd.Parameters.AddWithValue("@thirdweek", TextBox7.Text);


                    cmd.CommandText = "UPDATE addsetting SET tostudent=@tostudent,tofaculty=@tofaculty,studentday=@studentday,facultyday=@facultyday,firstweek=@firstweek,secondweek=@secondweek,thirdweek=@thirdweek WHERE rowno=@rowno";
                    cmd.ExecuteNonQuery();

                }

                conn.Close();
4

3 回答 3

5

您是否正在检查page_load 中的IsPostBack属性?或者无论是否有回发,您概述的代码是否都会运行?

如果代码运行而不管回发,那么您所做的是在您的 Button2_Click 有机会访问它们之前覆盖 TextBox 值。这是因为Load 在事件处理之前运行的页面生命周期。

如果不是回发(即用户没有单击提交按钮),您可能只希望 page_load 中的数据库检索运行,例如:

if (!Page.IsPostBack)
{
    using (SqlConnection conn = new SqlConnection("myconnectionString"))
    {
        conn.Open();
        using (SqlCommand cmmnd = new SqlCommand("", conn))
        {
            cmmnd.CommandText = "SELECT * FROM addsetting;";
            SqlDataReader rdr = cmmnd.ExecuteReader();

            while (rdr.Read())
            {
                count++;
                param = Convert.ToString(rdr["rowno"]);
                TextBox1.Text = Convert.ToString(rdr["tostudent"]);
                TextBox2.Text = Convert.ToString(rdr["tofaculty"]);
                TextBox3.Text = Convert.ToString(rdr["studentday"]);
                TextBox4.Text = Convert.ToString(rdr["facultyday"]);
                TextBox5.Text = Convert.ToString(rdr["firstweek"]);
                TextBox6.Text = Convert.ToString(rdr["secondweek"]);
                TextBox7.Text = Convert.ToString(rdr["thirdweek"]);
            }

            rdr.Close();
        }
    }
}
于 2013-03-22T02:35:24.087 回答
1
if(!IsPostBack)
{
    using (SqlConnection conn = new SqlConnection("myconnectionString"))
    {
        conn.Open();
        using (SqlCommand cmmnd = new SqlCommand("", conn))
        {



            cmmnd.CommandText = "SELECT * FROM addsetting;";


            SqlDataReader rdr = cmmnd.ExecuteReader();
            while (rdr.Read())
            {
                count++;
                param = Convert.ToString(rdr["rowno"]);
                TextBox1.Text = Convert.ToString(rdr["tostudent"]);
                TextBox2.Text = Convert.ToString(rdr["tofaculty"]);
                TextBox3.Text = Convert.ToString(rdr["studentday"]);
                TextBox4.Text = Convert.ToString(rdr["facultyday"]);
                TextBox5.Text = Convert.ToString(rdr["firstweek"]);
                TextBox6.Text = Convert.ToString(rdr["secondweek"]);
                TextBox7.Text = Convert.ToString(rdr["thirdweek"]);

            }
            rdr.Close();
        }
        conn.Close();}

}
于 2013-03-22T06:40:11.107 回答
0

这都是关于页面生命周期的。Page_Load 在处理 Click 事件之前触发。因此,当您单击 Button2 时,Page_Load 运行并使用旧值重新填充您的表单,然后 Button2.Click 被执行并将当前(现在旧)值保存回数据库。

于 2013-03-22T02:38:27.653 回答