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") %>' />
</EditItemTemplate>
<asp:FormView>
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, i.e.:
theListcontrol = CType(formView.FindControl(listControlName), ListControl)
…the data binding mechanism 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)
Ok, found “a” solution:
1. Call the DDL populate function from the
FormView_ItemCreatedevent.2. Modify the code to populate the DDL like so:
It kinda seems to basically boil down to, you cannot do additional “Databinding”, programatically at runtime, within a FormView.
(Anyone know why the formatting of my code is all screwed up in this post??)
Update
This solution raises yet another batch of issues, related to inserting new items. After a lot of screwing around I eventually found a way around that. At this point, I am basically at the point where I would recommend either avoiding the FormView control entirely, or definitely avoid programatically altering bound controls. Hopefully, perhaps using seperate data controls for each DropDownList might work better, but this might be wishful thinking as well.