【问题标题】:Binding a DropDownList in ListView InsertItemTemplate throwing an error在 ListView InsertItemTemplate 中绑定 DropDownList 会引发错误
【发布时间】:2009-01-22 18:33:51
【问题描述】:

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

【问题讨论】:

  • 当Dropdownlist在Listview中时,绑定在Dropdown的SelectedValue中不起作用...正如您所说的那样,您在另一个正在运行的网站中具有相同的设置...您是否仅在Listview中或在formview或gridview中放置了下拉列表???我也搜索了很多博客,因为我遇到了同样的问题。但我发现只有一个解决方案。 tht 使用 Eval 显示值并在 ItemInserting 和 itemUpdating 中“插入”或“更新”代码
  • 如果您找到任何解决方案。在列表视图中的下拉列表中进行绑定...请也提供给我...

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


【解决方案1】:

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

你是用&lt;%#Eval("field")%&gt;表达式绑定控件吗?如果 你把它改成&lt;%#DataBinder.Eval(Container.DataItem,"field")%&gt;, 这似乎可以解决错误?

【讨论】:

  • 是的!这是我的问题,我使用 'eval(666)' 来构造子控件,而 null 值会引发摇晃。我使用了&lt;%#DataBinder.Eval(Container.DataItem,"field")%&gt;,,它解决了这个问题,因为我的子控件处理了 null 并且没有像醉酒的猴子一样摔倒。 PS我更新了你的答案,因为只有链接的答案很糟糕......链接背后的东西可能有一天会消失。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

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

    来源: 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

    【讨论】:

      【解决方案4】:

      这会起作用

       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();
      
              }
          }
      

      【讨论】:

        【解决方案5】:

        我也遇到过类似的问题。如果您不更新该字段的值,请尝试使用#Eval。

        【讨论】:

          【解决方案6】:

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

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

          然后我在表单视图和下拉列表上都使用 DataBind(),一切都很好。

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

          我希望这会有所帮助 内森

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              猜你喜欢
              • 2013-02-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-05-15
              • 1970-01-01
              • 1970-01-01
              • 2011-05-30
              相关资源
              最近更新 更多