Not possible to load DropDownList on FormView from code behind??
Posted
by tbone
on Stack Overflow
See other posts from Stack Overflow
or by tbone
Published on 2010-03-12T19:19:53Z
Indexed on
2010/03/12
20:47 UTC
Read the original article
Hit count: 379
I have a UserControl, containing a FormView, containing a DropDownList. The FormView is bound to a data control.
Like so:
<asp:FormView ID="frmEdit" DataKeyNames="MetricCode" runat="server"
DefaultMode="Edit" DataSourceID="llbDataSource" Cellpadding="0" >
<EditItemTemplate>
<asp:DropDownList ID="ParentMetricCode" runat="server" SelectedValue='<%# Bind("ParentMetricCode") %>' />
etc, etc
I am trying to populate the DropDownList from the codebehind. If this was not contained in a FormView, I would normally just do it in the Page_Load event. However, that does not work within a FormView, as as soon as I try to do it, accessing the dropdownlist in code, ie:
theListcontrol = CType(formView.FindControl(listControlName), System.Web.UI.WebControls.ListControl)
...the data binding mechansim of the FormView is invoked, which, of course, tries to bind the DropDownList to the underlying datasource, causing a *'ParentMetricCode' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value * error, since the DropDownList has not yet been populated.
I tried performing the load in the DataBinding() event of the FormView, but then:
theListcontrol = CType(formView.FindControl(listControlName), System.Web.UI.WebControls.ListControl)
...fails, as the FormView.Controls.Count = 0 at that point.
Is this impossible? (I do not want to have to use a secondary ObjectDataSource to bind the dropdownlist to)
© Stack Overflow or respective owner