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

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

问题描述

我有我的表格上2 的DropDownList 控制,第二个使用它的的SelectedValue 第一为一体其绑定参数。

两者的DropDownList 控件是在 FormView.InsertItemTemplate 的SelectedValue 绑定到使用绑定防爆pression的 FormView控件的数据源属性。

第一次 FormView控件呈现在插入模式,一切工作正常。问题是的AutoPostBack 第一个的DropDownList FormView控件不(再)绑定,但因为 ControlParameter 第二的DropDownList 发生了变化,它 FormView控件上通过不具约束力假设p>


  

System.InvalidOperationException:数据绑定方法如的eval()
  的XPath()和绑定()只能在数据绑定的上下文中使用
  控制。


下面是标记:

 <&InsertTemplate则GT;



&所述; TR类=GridViewRowB>
                    < TD类=GridViewCell>
                        攻防型
                    < / TD>
                    < TD类=GridViewCell>
                        < ASP:DropDownList的ID =ddlOffenseType=服务器的DataSourceID =dsOffenseType
                            的AutoPostBack =真DataValueField =OffenseTypeIDDataTextField =说明
                            的SelectedValue ='<%#绑定(OffenseTypeID)%>'>
                        < / ASP:DropDownList的>
                        < ASP:ObjectDataSource控件ID =dsOffenseType=服务器的TypeName =OffenseType
                            SelectMethod =GETALL>
                            < SelectParameters>
                                < ASP:参数名称=ActiveOnly默认值=真类型=布尔/>
                            < / SelectParameters>
                        < / ASP:ObjectDataSource控件>
                    < / TD>
                < / TR>
                &所述; TR类=GridViewRowA>
                    < TD类=GridViewCell>
                        律师
                    < / TD>
                    < TD类=GridViewCell>
                        < ASP:DropDownList的ID =ddlAttorney=服务器的DataSourceID =dsAttorneyDataValueField =AttorneyID
                            DataTextField =AttorneyNameWithCount的SelectedValue ='<%#绑定(AttorneyID)%>'>
                        < / ASP:DropDownList的>
                        < ASP:ObjectDataSource控件ID =dsAttorney=服务器的TypeName =检察
                            SelectMethod =GetAttorneyWithCaseCount>
                            < SelectParameters>
                                < ASP:参数名称=ActiveOnly默认值=真类型=布尔/>
                                < ASP:ControlParameter NAME =OffenseTypeIDTYPE =的Int32控件ID =ddlOffenseType
                                    属性名=的SelectedValue/>
                            < / SelectParameters>
                        < / ASP:ObjectDataSource控件>
                    < / TD>
                < / TR>



< / InsertTemplate则>

我的问题是:什么是让此功能工作的最佳方法?是否可以保留两个DDL的模板里面呢?我想preFER避免使用AJAX工具包或其他客户端解决方案。


解决方案

这是当我们使用级联的DropDownList的数据绑定控件像 DetailsView控件/ FormView控件一个问题,我曾经面临很多次。你必须从你的第二个DROPDOWNLIST删除绑定防爆pression 的SelectedValue ='<%#绑定(AttorneyID)%> ,那么它会工作

其次,如果你删除绑定前pression,你必须手动传递值在FormView控件 ItemInserting 事件。例如。

 保护无效frmAsset_ItemInserting(对象发件人,FormViewInsertEventArgs E)
 {
    eValues​​ [AttorneyID] =((DropDownList的)((FormView控件)发送方).FindControl(ddlAttorny))的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.

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.

解决方案

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.

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天全站免登陆