绑定ListView中的DropDownList InsertItemTemplate抛出错误

时间:2009-01-22 18:33:51

标签: asp.net linq-to-sql data-binding

我有一个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>

7 个答案:

答案 0 :(得分:5)

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

  

您使用<%#Eval("field")%>表达式来绑定控件吗?如果   你将其更改为<%#DataBinder.Eval(Container.DataItem,"field")%>,   这似乎可以解决错误吗?

答案 1 :(得分:2)

看一下这个帖子http://forums.asp.net/t/1187425.aspx

基本上你不能使用Bind语法,因为InsertItemTemplate中的控件只是“单向”。使用后面的代码来获取值。

答案 2 :(得分:1)

对于这种情况,我们可以考虑以下决议:

  1. 对于内部嵌套控件,我们使用其他方法来填充其项目,而不是数据绑定。例如,我们可以使用for循环将项添加到下拉列表中而不是数据绑定。因此,&lt;%#...%&gt;表达式不会被评估。

  2. 我们还可以对容器DetailsView控件执行更改。而不是&lt;%#%&gt;表达式,我们可以手动使用一些事件,如“RowDataBound”,将数据项填充到某个内部控件。执行更新/插入时,在适当的事件中手动提取控件中的值(例如ItemUpdating或ItemInserting ...)。

  3. 源: http://blogs.msdn.com/b/stcheng/archive/2009/01/15/aspnet-system-invalidoperationexception-databinding-methods-such-as-eval-xpath-and-bind-can-only-be-used-in-the-context-of-a-databound-control-error-in-nested-databinding-scenario.aspx

答案 3 :(得分:1)

这将起作用

 protected void lstCompContctPerson_ItemCreated(object sender, RadListViewItemEventArgs e)
        {

        if ((e.Item != null) && (e.Item.ItemType == RadListViewItemType.InsertItem))
        {
            System.Web.UI.Control ddlCompNames = e.Item.FindControl("ddlCompNames");

           (ddlCompNames as RadDropDownList).DataSource = (from _company in objshortcut.GetAllCompanies()
            select new
            {
                  CompanyID = _company.CompanyID,
                  CompContctName = _company.CompanyName
             }).Distinct();

        }
    }

答案 4 :(得分:0)

我遇到了类似的问题。如果您没有更新该字段的值,请尝试使用#Eval代替。

答案 5 :(得分:0)

我已经成功了。我有一个带有SqlDataSource的Formview,在表单视图中有一个SQLdataSource的下拉列表。

当请求新记录时,我重新发出两个数据源的select命令 - 下拉列表源提供选项列表,而formview数据源不返回任何内容。

然后我在formview和下拉列表中使用DataBind(),一切都很顺利。

我是ASP.Net的新手,但这对我有用,让我可以做我想做的事 - 在下拉列表和formview上的文本框中使用Bind

我希望这会有所帮助 森

答案 6 :(得分:0)

使用#Eval可能值得使用DataBinder.Eval()来确保在正确的时间进行绑定。