2013-04-03 106 views
1

我有一个下拉列表与数据库表中的数据,我想插入与此ddl + 2 texbox字段在另一个表中。插入工作,但总是插入首先默认的dropdownList女巫显示。我错过了什么?dropdownlist插入总是默认值

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; 

      } 

回答

2

每当你按ASP:按钮的页面被重新加载,和page_load方法中的代码会再次被初始化。这就是为什么默认项目总是插入到数据库中的原因。

在Page_Load将这个在你的代码:(如果(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(); 

        } 
       } 
       } 

      } 
4

你再结合每个页面加载列表。将数据绑定代码包装在if (!IsPostBack)块中。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     // Bind the list here... 
    } 
}