在模板化控件中实现级联 DropDownList 绑定 [英] Implementing cascading DropDownList binding in a templated control

查看:17
本文介绍了在模板化控件中实现级联 DropDownList 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单上有 2 个 DropDownList 控件,其中第二个控件使用第一个的 SelectedValue 作为其绑定参数之一.

I have 2 DropDownList controls on my form, the second of which uses the SelectedValue of the first as one of its binding parameters.

两个 DropDownList 控件都在 FormView.InsertItemTemplate 中,SelectedValue 属性绑定到 FormView 的数据源使用绑定表达式.

Both DropDownList controls are in a FormView.InsertItemTemplate with SelectedValue properties bound to the FormView's datasource using a Binding Expression.

第一次 FormView 在插入模式下呈现时,一切正常.问题是在第一个 DropDownListAutoPostBack 之后,FormView 不会(重新)绑定,但是由于 ControlParameter 在第二个 DropDownList 已经改变,它确实绑定(按预期),但第二个 DDL 的绑定表达式发生异常,我假设自从 FormView> 对该通行证没有约束力:

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:Eval() 等数据绑定方法,XPath() 和 Bind() 只能在数据绑定的上下文中使用控制.

System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

这里是标记:

<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 工具包或其他客户端解决方案.

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.

推荐答案

当我们在 DetailsView/FormView 等数据绑定控件中使用级联下拉列表时,这是一个问题,我遇到过很多次.您必须从第二个下拉列表 SelectedValue='<%# Bind("AttorneyID") %>' 中删除绑定表达式,然后它就会起作用.

This is an issue when we use cascading dropdownlist in Databinding Controls like DetailsView/FormView and I have faced it many times. You have to remove the Binding Expression from your Second Dropdownlist SelectedValue='<%# Bind("AttorneyID") %>', then it will work.

其次,如果您删除了 Binding 表达式,则必须在 FormView ItemInserting 事件中手动传递该值.例如

Secondly if you remove the Binding expression, you have to pass the value manually in FormView ItemInserting Event. e.g.

 protected void frmAsset_ItemInserting(object sender, FormViewInsertEventArgs e)
 {
    eValues["AttorneyID"] = ((DropDownList)((FormView)sender).FindControl("ddlAttorny")).SelectedValue;
 }

这篇关于在模板化控件中实现级联 DropDownList 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆