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)
Implement the
OnDataBinding
directly on yourDropDownList
.When you bind your formview to some data the
OnDataBinding
for theDropDownList
will fire. At this point you can then load the the values you want into the list and bind the selected value to the loaded list as well.Here is an example:
Then implement the OnDataBinding:
When
DataBind
your FormView, everything will start to do its magic :)Also if you loading the list data for your
DropDownList
from a DB or something you might want to cache it since each 'row' of data will cause the loading of the list data.EDIT: Since you think it's not possible I wrote a quick demo app to prove how it works:
In your aspx file include this:
Then in your .cs file:
Run the code... the
DropDownList
will be loaded with all the values and be set to the correct selected value that was set in the mocked object.Not sure what else I can do to prove it any better... I think your missing how DataBinding actually works. If you try to do it at the
FormView
level, the controls don't get made until theFormView
is actually bound. When the binding happens, you can trigger each control within the template to do something by implementing it'sOnDataBinding
event. At that point the current iteration of bound object and it's values are available and that is where you see my code do anEval("ID")
.Ok, found "a" solution:
1. Call the DDL populate function from the
FormView_ItemCreated
event.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.
It is working for me with the OnDataBound event instead of OnDataBinding which occurs before the control is created.
Code-behind:
If you use this on other templates than the Edit you just need to validate: