【发布时间】:2011-07-19 15:38:17
【问题描述】:
我的表单上有 2 个DropDownList 控件,其中第二个使用第一个的SelectedValue 作为其绑定参数之一。
两个DropDownList 控件都在FormView.InsertItemTemplate 中,SelectedValue 属性使用绑定表达式绑定到FormView 的数据源。
FormView 第一次以插入模式呈现时,一切正常。问题是在第一个DropDownList 的AutoPostBack 之后,FormView 不会(重新)绑定,但是由于第二个DropDownList 上的ControlParameter 已更改,它确实绑定(按预期) ,但第二个 DDL 的绑定表达式发生异常,我假设因为 FormView 在该通道上没有绑定:
System.InvalidOperationException:Eval()等数据绑定方法, XPath() 和 Bind() 只能在数据绑定的上下文中使用 控制。
这里是标记:
<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>
我的问题是:使此功能发挥作用的最佳方法是什么?是否可以将两个 DDL 都保留在模板中?我宁愿避免使用 AJAX 工具包或其他客户端解决方案。
【问题讨论】:
标签: asp.net drop-down-menu formview