微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net – 绑定ListView InsertItemTemplate中的DropDownList抛出错误

我有一个ListView绑定到 LinqDataSource并显示选定的位置.插入项包含一个下拉列表,该列表从另一个LinqDataSource拉出以提供所有未选择的位置.

问题是加载页面时出现以下错误

数据绑定方法(如Eval(),XPath()和Bind())只能在数据绑定控件的上下文中使用.

我在网站的另一个页面上做了一个非常类似的设置,并没有给我们这个错误,所以我很困惑.我知道我可以通过不绑定,手动查找控件和获取值来解决这个问题,但这应该工作,我不明白为什么不是.

有什么想法吗?

代码的更好部分如下.

<asp:LinqDataSource ID="ldsLocations" runat="server" 
    ContextTypeName="ClearviewInterface.ESLinqDataContext" EnableDelete="true" EnableInsert="true"
    OnInserting="ldsLocations_Inserting" OnDeleting="ldsLocations_Deleting" 
    TableName="crmlocations" OrderBy="addr1" OnSelecting="ldsLocations_Selecting" />

<asp:LinqDataSource ID="ldsFreeLocations" runat="server" 
    ContextTypeName="ClearviewInterface.ESLinqDataContext" OrderBy="addr1" 
    TableName="v_CVLocations" OnSelecting="ldsFreeLocations_Selecting" />

<asp:ListView ID="lvLocations" DataSourceID="ldsLocations" DataKeyNames="ID" InsertItemPosition="LastItem" runat="server" >

<InsertItemTemplate>
        <tr>
            <td colspan="6"><hr /></td>
        </tr>
        <tr>
            <td colspan="2">

                <asp:DropDownList    ID="ddlFreeLocations" DataSourceID="ldsFreeLocations" DataTextField="addr1" 
                                        DataValueField="record" MarkFirstMatch="true" SelectedValue='<%# Bind("record") %>' 
                                        runat="server" />
            </td>
            <td><asp:ImageButton ID="btnAdd" CommandName="Insert" SkinID="Insert" runat="server" /></td>
        </tr>

    </InsertItemTemplate>

解决方法

正如 Joteke’s Blog所解释的,这是一个引用的解决方案.

Are you using <%#Eval("field")%> expression to bind the control? If
you change that to <%#DataBinder.Eval(Container.DataItem,"field")%>,does that seem to fix the error?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐