【问题标题】:DataBinder.Eval error in asp.netasp.net 中的 DataBinder.Eval 错误
【发布时间】:2017-03-14 21:56:21
【问题描述】:

我创建了一个类来完成一些工作,例如从 System.Web.UI.WebControls.WebControl 继承的 GridView。

public class IHGridView : System.Web.UI.WebControls.WebControl
{
    // inside here, actually return Repeater class.


    protected override void OnInit(EventArgs e)
    {
        _repeater.ItemTemplate = new IHGridItemTemplate(ListItemType.Item, this.Columns);
        this.Controls.Add(_repeater);
    }
}

我还在 IHGridView 中为我的转发器创建了 ItemTemplate。

public class IHGridItemTemplate : ITemplate
{
}

IHGridView 类返回 Repeater 和一些 html 代码,但为了方便开发,我创建了一些东西。

public class Columns : StateManagedCollection
{
}

public class IHBoundFieldBase : IStateManager
{
}

public class IHLabelField : IHBoundFieldBase
{
}

现在在我的 aspx 中,我可以像下面这样使用它:

<cc1:IHGridView ID="IHGridView1" runat="server" EditMode="View">
    <Columns>
         <cc1:IHLabelField ID="IHLabelField7" DataField="PERSON_NAME" HeaderText="PersonName" />
    </Columns>
</cc1:IHGridView>

现在我想出了一个问题。 我不能在 aspx 中使用 DataBinder.Eval。

<cc1:IHLabelField ID="IHLabelField7" HeaderText="PersonName" Text='<%# DataBinder.Eval(Container.DataItem, "PERSON_NAME") %>' />

这给了我一个错误。 错误消息如下: CS1061:“System.Web.UI.Control”中没有“DataItem”的定义。 “System.Web.UI.Control”的第一个参数中没有可扩展的方法“DataItem”。请检查是否有使用量规或汇编参考。这是用韩文写的,但我翻译成英文。 谁能给我一个线索来解决这个问题?

【问题讨论】:

    标签: asp.net repeater databinder


    【解决方案1】:

    在模板化控件中,模板在容器中实例化。为了在模板化字段中进行数据绑定,建议容器应实现IDataItemContainer 接口 - 接口实现应提供数据项。

    AFAIK,为了支持数据绑定表达式,ASP.NET 解析器为控件(其属性使用这些表达式)注入 DataBinding 事件的处理程序,然后在处理程序中,它生成在容器中查找数据项的代码.

    因此,在您的示例中,如果您希望在 IHLabelField.Text 属性中使用数据绑定表达式,则控件的命名容器应实现 IDataItemContainer 或应具有 DataItem 属性。所以在这种情况下,您可能需要在IHGridView 控件上使用DataItem - 它不会按照您想要的方式工作。

    【讨论】:

      【解决方案2】:

      这是我们使用的一个示例。希望对你有帮助

         &lt;asp:HyperLink ID="phoneManagementHyperLink" runat="server" Text='&lt;%# (Container.DataItem as WcfUser).firstName + " " + (Container.DataItem as WcfUser).lastName%&gt;'

      【讨论】:

        猜你喜欢
        • 2010-12-13
        • 2011-11-29
        • 2011-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多