I have 2 DropDownList
controls on my form, the second of which uses the SelectedValue
of the first as one of its binding parameters.
Both DropDownList
controls are in a FormView.InsertItemTemplate
with SelectedValue
properties bound to the FormView
's datasource using a Binding Expression.
The first time the FormView
renders in Insert mode, everything works fine. The problem is after an AutoPostBack
from the first DropDownList
, the FormView
doesn't (re-)bind, however since the ControlParameter
on the second DropDownList
has changed, it DOES bind (as intended), but an exception occurs on the Binding Expression of the second DDL, I assume since the FormView
is not binding on that pass:
System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Here is the markup:
<InsertItemTemplate>
.
.
.
<tr class="GridViewRowB">
<td class="GridViewCell">
Offense Type
</td>
<td class="GridViewCell">
<asp:DropDownList ID="ddlOffenseType" runat="server" DataSourceID="dsOffenseType"
AutoPostBack="true" DataValueField="OffenseTypeID" DataTextField="Description"
SelectedValue='<%# Bind("OffenseTypeID") %>'>
</asp:DropDownList>
<asp:ObjectDataSource ID="dsOffenseType" runat="server" TypeName="OffenseType"
SelectMethod="GetAll">
<SelectParameters>
<asp:Parameter Name="ActiveOnly" DefaultValue="True" Type="Boolean" />
</SelectParameters>
</asp:ObjectDataSource>
</td>
</tr>
<tr class="GridViewRowA">
<td class="GridViewCell">
Attorney
</td>
<td class="GridViewCell">
<asp:DropDownList ID="ddlAttorney" runat="server" DataSourceID="dsAttorney" DataValueField="AttorneyID"
DataTextField="AttorneyNameWithCount" SelectedValue='<%# Bind("AttorneyID") %>'>
</asp:DropDownList>
<asp:ObjectDataSource ID="dsAttorney" runat="server" TypeName="Attorney"
SelectMethod="GetAttorneyWithCaseCount">
<SelectParameters>
<asp:Parameter Name="ActiveOnly" DefaultValue="True" Type="Boolean" />
<asp:ControlParameter Name="OffenseTypeID" Type="Int32" ControlID="ddlOffenseType"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:ObjectDataSource>
</td>
</tr>
.
.
.
</InsertItemTemplate>
My question is: What is the best way to make this functionality work? Is it possible to keep both DDL's inside the template? I would prefer to avoid using the AJAX toolkit or other client-side solutions.
FormView.ItemInserting
and add the parameter in manually? What about the same issue inEdit
mode? Handle both the initial and updated values manually? Example code for posterity would be appreciated. – Sinus