Problem doing DataBind on a control within a DataList?

  • Thread starter Thread starter Olav Tollefsen
  • Start date Start date
O

Olav Tollefsen

I have a Web Form with a DataList. Inside the ItemTemplate, I have a
DropDownList control.

<asp:DataList ID="ProductDataList" Runat="server">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" Runat="server"
DataTextField="PropertyValue" DataValueField="PropertyValueId">
</asp:DropDownList>
</ItemTemplate>
</asp:DataList>

In the Page_Load for the Web Form, I do the following:

1) First I bind the DataList control to a data source.

ProductDataList.DataSource = product;
ProductDataList.DataBind();

2) Then I try to bind the DropDownList control to a data source:

DropDownList dropdown1 =
(DropDownList)ProductDataList.Items[0].FindControl("DropDownList1");
dropdown1.DataSource = propertyValues;
dropdown1.DataBind();

The code executes fine and I can see that the data source in step 2 is
containing the correct values that should be displayed in the DropDownList
control.

The problem is that the DropDownList control is empty. If I do exactly the
same with a DropDownList control that is placed on the Web Form outside the
DataList control, everything works just as expected.

Any hints on how to solve this problem?

Olav
 
Olav,

It seems to me that you should be doing the databinding the dropdownlists in
the datalist ItemDataBound event.

Eliyahu
 
Moving the code to the ItemDataBound event handler didn't make any
difference. Seems like the problem is related to using the Visual Studio
2005 Beta 1 refresh. I tested with Visual Studio 2004 and then it worked as
expected.

Olav

Eliyahu Goldin said:
Olav,

It seems to me that you should be doing the databinding the dropdownlists
in
the datalist ItemDataBound event.

Eliyahu

Olav Tollefsen said:
I have a Web Form with a DataList. Inside the ItemTemplate, I have a
DropDownList control.

<asp:DataList ID="ProductDataList" Runat="server">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" Runat="server"
DataTextField="PropertyValue" DataValueField="PropertyValueId">
</asp:DropDownList>
</ItemTemplate>
</asp:DataList>

In the Page_Load for the Web Form, I do the following:

1) First I bind the DataList control to a data source.

ProductDataList.DataSource = product;
ProductDataList.DataBind();

2) Then I try to bind the DropDownList control to a data source:

DropDownList dropdown1 =
(DropDownList)ProductDataList.Items[0].FindControl("DropDownList1");
dropdown1.DataSource = propertyValues;
dropdown1.DataBind();

The code executes fine and I can see that the data source in step 2 is
containing the correct values that should be displayed in the
DropDownList
control.

The problem is that the DropDownList control is empty. If I do exactly
the
same with a DropDownList control that is placed on the Web Form outside the
DataList control, everything works just as expected.

Any hints on how to solve this problem?

Olav
 
Back
Top