1

我有一个包含数据库表数据的下拉列表,我想在另一个表中插入这个 ddl + 2 texbox 字段。插入工作但始终插入默认的下拉列表女巫首先显示。我错过了什么?

 protected void Page_Load(object sender, EventArgs e)
            {


                string connectionString = cs.getConnection();
                string query = "SELECT Id, NAME FROM PROFITCATEGORIES";
                using (SqlConnection myConnection = new SqlConnection(connectionString))
                {
                    myConnection.Open();
                    SqlCommand command = new SqlCommand(query, myConnection);
                    using (SqlDataReader rdr = command.ExecuteReader())
                    {

                        DropDownListCategory.DataSource = rdr;
                        DropDownListCategory.DataTextField = "Name";
                        DropDownListCategory.DataValueField = "ID";
                        DropDownListCategory.DataBind();

                    }
                }

            }

            protected void ButtonSave_Click(object sender, EventArgs e)
            {
                if (String.IsNullOrWhiteSpace(TextBoxValue.Text))
                    return;
                string connectionString = cs.getConnection();
                string insertSql = "INSERT INTO profits(Value,DateCreate,IdCategory,IdUser) VALUES(@Value, @DateCreate,@CategoryId,@UserId)";

                using (SqlConnection myConnection = new SqlConnection(connectionString))
                {
                    myConnection.Open();
                    SqlCommand command = new SqlCommand(insertSql, myConnection);
                    //параметризация се прави тук за да се избегне SQL Injection
                    command.Parameters.AddWithValue("@Value", TextBoxValue.Text);
                    command.Parameters.AddWithValue("@DateCreate", TextBoxData.Text);
                    command.Parameters.AddWithValue("@CategoryId", DropDownListCategory.SelectedValue);
                    command.Parameters.AddWithValue("@UserId", cui.getCurrentId());
                    command.ExecuteNonQuery();
                    //пренасочваме заявката към същата страница за да се видят новите резултати и да се избегне проблем с дублиране на инсерт при рефреш на страницата
                    Response.Redirect("~/Profit.aspx");
                    myConnection.Close();
                }
                TextBoxValue.Text = string.Empty;

            }
4

3 回答 3

4

您在每次页面加载时重新绑定列表。将数据绑定代码包装在一个if (!IsPostBack)块中。

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      // Bind the list here...
   }
}
于 2013-04-03T19:37:36.997 回答
2

每次按下 asp:button 时,页面都会重新加载,并且 page_load 方法中的代码会再次初始化。这就是为什么默认项总是插入到您的数据库中。

将其放在 page_load 中的代码周围:(if (!Page.IsPostback))

 protected void Page_Load(object sender, EventArgs e)
            { 

               if (!Page.isPostback)
                {
                string connectionString = cs.getConnection();
                string query = "SELECT Id, NAME FROM PROFITCATEGORIES";
                using (SqlConnection myConnection = new SqlConnection(connectionString))
                {
                    myConnection.Open();
                    SqlCommand command = new SqlCommand(query, myConnection);
                    using (SqlDataReader rdr = command.ExecuteReader())
                    {

                        DropDownListCategory.DataSource = rdr;
                        DropDownListCategory.DataTextField = "Name";
                        DropDownListCategory.DataValueField = "ID";
                        DropDownListCategory.DataBind();

                    }
                }
              }

            }
于 2013-04-03T19:36:48.727 回答
1

使用 AppendDataBoundItems 属性并将其设置为 true,并且仅在 PostBack 上绑定:

如何在 ASP.NET 中向 Databound DropDownList 控件添加附加项?

ListControl.AppendDataBoundItems 属性

于 2013-04-03T19:34:49.203 回答