c# - I need to get the value of a drop down list in an asp gridview when updating the row -


everything should work, can't figure out why can't value ddl. know code not clean.

protected void gridproduse_rowediting(object sender, gridviewediteventargs e)     {         gridproduse.editindex = e.neweditindex;         gridproduse.databind();           using (var context = new satcontext())         {             var query = 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; // error         //prod.moneda = ((row.findcontrol("ddlmoneda") 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");     } 

and error:

an exception of type 'system.nullreferenceexception' occurred in licenta.dll not handled in user code

additional information: object reference not set instance of object.

i'm pretty sure need select cell before can reliably call `findcontrol'

(dropdownlist)gridproduse.rows[e.rowindex].cells[somecell].findcontrol("ddlmoneda"); 

since listmoneda control being created dynamically after page_init event, not being registered in viewstate, , cannot found on postback. read explanation solution


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -