一切都應該工作,但我無法弄清楚爲什麼我無法從ddl獲取值。我知道代碼不太乾淨。我需要在更新行時獲取asp gridview中下拉列表的值
protected void gridProduse_RowEditing(object sender, GridViewEditEventArgs e)
{
gridProduse.EditIndex = e.NewEditIndex;
gridProduse.DataBind();
using (var context = new SATContext())
{
var query = from t in context.TipuriProduse
select t.Denumire;
DropDownList list = new DropDownList();
list.DataSource = query.ToList();
list.DataBind();
list.ID = "ddlTipProdus";
list.Height = 27;
DropDownList listMoneda = new DropDownList();
listMoneda.ID = "ddlMoneda";
listMoneda.Items.Add("RON");
listMoneda.Items.Add("EUR");
listMoneda.Items.Add("USD");
listMoneda.Height = 27;
gridProduse.Rows[e.NewEditIndex].Cells[7].Controls.Add(list);
gridProduse.Rows[e.NewEditIndex].Cells[6].Controls.Add(listMoneda);
gridProduse.Rows[e.NewEditIndex].Cells[6].Controls[0].Visible = false;
gridProduse.Rows[e.NewEditIndex].Cells[7].Controls[0].Visible = false;
}
}
protected void gridProduse_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gridProduse.Rows[e.RowIndex];
Produs prod = new Produs();
prod.ProdusId = Convert.ToInt32(((TextBox)(row.Cells[2].Controls[0])).Text);
prod.Denumire = ((TextBox)(row.Cells[3].Controls[0])).Text;
DropDownList ddl = (DropDownList)gridProduse.Rows[e.RowIndex].FindControl("ddlMoneda");
prod.Moneda = ddl.SelectedValue; // this is where i get the error
//prod.Moneda = ((row.FindControl("ddlMoneda") as DropDownList)).SelectedValue;
prod.PretCuTVA = Convert.ToInt32(((TextBox)(row.Cells[5].Controls[0])).Text);
prod.PretFaraTVA = Convert.ToInt32 (((TextBox)(row.Cells[4].Controls[0])).Text);
lit1.Text = prod.ProdusId.ToString();
using (var context = new SATContext())
{
IRepository<Produs> ProdusRepository = new ProdusRepository();
ProdusRepository.Update(prod);
}
gridProduse.EditIndex = -1;
gridProduse.DataBind();
Response.Redirect("Produse.aspx");
}
這是錯誤:
類型「System.NullReferenceException」的一個例外發生在Licenta.dll但在用戶代碼中沒有處理
其他信息:對象沒有設置到一個對象的實例。
我想現在沒有'SelectedValue'。你確定選擇了一行或一個項目嗎? –
可能的重複[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – dotctor
是的,我很確定它被選中。我知道是什麼NullReferenceException,我只是不知道爲什麼它不起作用... –