我在模板字段中有一個下拉列表,它在加載時將選定的值設置爲gridview中的另一個字段。在rowupdating事件中,下拉列表返回到初始選定值並且不保留新值。我有一個方法來填充包裝在page_load事件中的if this.page.IsPostBack中的網格。如何在rowupdating事件期間綁定控件的新選定值並忽略它的初始數據綁定?在模板字段中的下拉列表中選擇的值不會更改
我將auto post back屬性設置爲true。
這裏是RowUpdating事件(其實我插入到不同的表不更新該網格)
protected void grd_add_bins__RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)grd_add_bins.Rows[e.RowIndex];
//int id = Int32.Parse(grdBins.DataKeys[e.RowIndex].Value.ToString());
Label room1 = (Label)row.FindControl("grd_add_room");
Label grower1 = (Label)row.FindControl("grd_add_grower_id");
TextBox bins1 = (TextBox)row.FindControl("grd_add_txtbins_to_pack");
DropDownList packas1 = (DropDownList)row.FindControl("grd_add_ddl_pack_as");
TextBox block1 = (TextBox)row.FindControl("grd_add_txt_block");
Label pool1 = (Label)row.FindControl("grd_add_pool");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into t_run_schedule_lots " +
"(record_key,room_id,grower_id,block_id,pool_id,total_bins,pack_as) " +
"values (@rec_id,@room,@grower,@block,@pool,@bins,@pack_as)";
cmd.Parameters.Add("@rec_id", SqlDbType.VarChar).Value = lblRec_key.Text;
cmd.Parameters.Add("@room", SqlDbType.VarChar).Value = room1.Text;
cmd.Parameters.Add("@grower", SqlDbType.VarChar).Value = grower1.Text;
cmd.Parameters.Add("@bins", SqlDbType.Int).Value = Convert.ToInt32(bins1.Text);
cmd.Parameters.Add("@pack_as", SqlDbType.VarChar).Value = packas1.Text;
cmd.Parameters.Add("@block", SqlDbType.VarChar).Value = block1.Text;
cmd.Parameters.Add("@pool", SqlDbType.VarChar).Value = pool1.Text;
cmd.CommandType = CommandType.Text;
cmd.Connection = this.sqlConnection1;
this.sqlConnection1.Open();
// execute insert statement
cmd.ExecuteNonQuery();
this.sqlConnection1.Close();
fill_grid();<-- fills other grid that shows the result of the insert
fill_bins_grid();<-- fills this grid
//Reset the edit index.
grd_add_bins.EditIndex = -1;
//Bind data to the GridView control.
grd_add_bins.DataBind();
}
在初始頁面加載中綁定數據網格。所以修改你的頁面加載代碼以使用!IsPostBack。您的代碼在每次回發後都會綁定數據。 – Tariqulazam
好吧,因此,從你說的所有事情看,ddl'grd_add_ddl_pack_as'改變了值,頁面回傳,插入到另一個表中,重新填充數據表,然後重新綁定。但是你希望'grd_add_ddl_pack_as'保持新的'Selected'值?在我看來,回發和重新綁定後,它將會有'fill_bins_grid'返回的那條記錄和列作爲選定的值。 –
是安,這是正確的。但是ddl會返回到它綁定到的字段的選定值。 – briskovich