Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating dropdownlist in datalist

Status
Not open for further replies.

TheConeHead

Programmer
Aug 14, 2002
2,106
US
I have a datalist in my edittemplate that I am currently trying to populate from the codebehind and I am getting an error saying that the object (dropdownlist) does not exist... the naming is all correct... is it possible to populate it from the codebehind?? Is it hitting this code before it is actually creating the dropdownlist?

confused....

[conehead]
 
you have to do it in the code behind...

you need to use FindControl() inside DataListCommandEventArgs (update click)to find your datagrid.

-DNG
 
Follow the example in the thread you posted, just add a check to see if the row is in edit mode
Code:
Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        [b]If e.Item.ItemType = ListItemType.EditItem[/b] Then
           'find  the object and populate it
        End If
    End Sub

Jim
 
ok - I think I am geting it - give that this code is in a function that is called from within Edit_Command, how would I convert the follwoing to c#

Code:
Dim ddlEditcategory as DropDownList = CType(e.Item.FindControl("ddlEditcategory"),DropDownList)

[conehead]
 
if i am correct then this is the converted code:

DropDownList ddlEditcategory = (DropDownList)e.Item.FindControl("ddlEditcategory");



Known is handfull, Unknown is worldfull
 
ok - truied that and that looks right, but the next line in my code:

Code:
ddlEditCategory.DataSource = dlreader;


is getting the following error still:

Object reference is not set to an instance of an object

??????


[conehead]
 
means that the control was NOT found. are you sure that the control is there in the grid???

Known is handfull, Unknown is worldfull
 
It's definetly there... it seems like this function is being called before the dropdownlist is being drawn... If I do not try to populate it (i.e. ddlEditCategory.DataSource = dlreader;) it makes the dropdown just fine - it is when I try to populate it that it breaks - your right - it seems like it is not there, but it should be....

[conehead]
 
are you sure you are doing this inside the ItemDataBound event and checking for the ListItemType being equal to EditItem as Jim pointed out...also not sure this is the problem but put extra parenthesis as shown below:

DropDownList ddlEditcategory = (DropDownList)(e.Item.FindControl("ddlEditcategory"));

-DNG
 
probably not - can some some C# this code for me (yes, I'm helpless)

Code:
Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        If e.Item.ItemType = ListItemType.EditItem Then
           'find  the object and populate it
        End If
    End Sub

As I am doing it now, it is simply in the Edit_Command function....

[conehead]
 
Great site - a definite star for that when this thread wraps up!!

[conehead]
 
OK - I've got it converted to:

Code:
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) 
{ 
 if (e.Item.ItemType == ListItemType.EditItem) { 
 } 
}

but it does not seem to be being called - how do I call it?

[conehead]
 
Code:
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) 
{ 
 if (e.Item.ItemType == ListItemType.EditItem) { 
DropDownList ddlEditcategory = (DropDownList)(e.Item.FindControl("ddlEditcategory"));
ddlEditCategory.DataSource = dlreader;

 } 
}

-DNG
 
Sorry - yeah - I've got all of that in there, but DataList1_ItemDataBound does not seem to be called at any point... What qould cause it to be called?

[conehead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top