How to initialize dropdownlist value in a gridview column edit template

K

keithb

I have dropdown list in a gridview column edit template. It works OK, except
when I edit a row, the dropdown list does not initialize to the existing
value that shows in the textbox that displays in the item template. I tried
to initialize the selected value of the dropdown list with some lines of
code in the grid_RowEditing event but I get an error that I do not
understand. The error occurs at this line of code:

String DelID =
((DropDownList)grid.Rows[e.NewEditIndex].FindControl("DelDropDownList")).Text;

the error is: "Object reference not set to an instance of an object."

I do not understand this, because code from the grid_RowUpdating event that
runs error-free is the same except that it uses e.RowIndex instead of
e.NewEditIndex and both indexes have the same value. Can someone shed some
light on this for me?

Thanks,

Keith
 
M

MartinDFisher

I think (and someone please correct me if I'm wrong) but all of the
controls inside the edit item template are not created until the data
grid is bound. Also the OnRowEditing event fires before the
OnRowDataBound event. Therefore during the on row editing event the
drop down list (and anything else inside the edit item template)
doesn't exist yet.

The way I got it to work for me when I ran into this problem is to
initialize the selected value of the drop down list during the
OnRowDataBound event- not the OnRowEditing event.
 
K

keithb

The solution turned was simple: I just needed to bind the SelectedValue
property of the DropDownList to the related field in the GridView edit
template. Now that I have done that, everything works.

Thanks,

Keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top