DropDownList template column in DataGrid

G

Guest

I have a DataGrid (WebForm - C#) that has a template column that contains a dropdownlist named "DdlTest"

In DataGrid1_UpdateCommand, the lin

DropDownList ddlTest = (DropDownList) e.Item.FindControl("DdlTest")

enables me to work with ddlTest - with, for example, ddlTest.SelectedValu

However, in DataGrid1_EditCommand, ddlTest.SelectedValue returns "Object reference not set to an instance of an object.

Is it possible to work with DdlTest in DataGrid1_EditCommand?
 
G

Guest

My goal is to have a dropdownlist in the template column of a datagrid come up with the last saved value selected, when the user clicks 'edit'
 
J

jm

if e.item.ItemType = ListItemType.EditItem then
.....


mg said:
I have a DataGrid (WebForm - C#) that has a template column that contains
a dropdownlist named "DdlTest" :
In DataGrid1_UpdateCommand, the line

DropDownList ddlTest = (DropDownList) e.Item.FindControl("DdlTest");

enables me to work with ddlTest - with, for example, ddlTest.SelectedValue

However, in DataGrid1_EditCommand, ddlTest.SelectedValue returns "Object
reference not set to an instance of an object."
Is it possible to work with DdlTest in DataGrid1_EditCommand?
if e.item.ItemType = ListItemType.EditItem then
.....


mg said:
I have a DataGrid (WebForm - C#) that has a template column that contains
a dropdownlist named "DdlTest" :
In DataGrid1_UpdateCommand, the line

DropDownList ddlTest = (DropDownList) e.Item.FindControl("DdlTest");

enables me to work with ddlTest - with, for example, ddlTest.SelectedValue

However, in DataGrid1_EditCommand, ddlTest.SelectedValue returns "Object
reference not set to an instance of an object."
 
J

jm

jm said:
if e.item.ItemType = ListItemType.EditItem then
....



a dropdownlist named "DdlTest" :
reference not set to an instance of an object."
if e.item.ItemType = ListItemType.EditItem then
....



a dropdownlist named "DdlTest" :
reference not set to an instance of an object."


mg,

I found another way also:

http://www.aspnet101.com/aspnet101/pfTutorials.aspx?id=4

"Selecting Exact Items
To select a particular item in the list, based on the text (visible)
item, you can use this code:

DropDownList.SelectedIndex =
DropDownList.Items.IndexOf(DropDownList.Items.FindByText("text"))

To select a particular item in the list, based on the Value of the
item, use this code:

DropDownList.SelectedIndex =
DropDownList.Items.IndexOf(DropDownList.Items.FindByValue("value"))

Code for a ListBox is a little simpler:

ListBox.Items.FindByText("Text").Selected = true
ListBox.Items.FindByValue("value").Selected = true"


These should be very helpful to you.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top