为什么将 GridView 代码移动到 ascx 给定“数据绑定表达式仅在具有 DataBinding 事件的对象上受支持"?例外? [英] Why has moving GridView code to an ascx given an "Databinding expressions are only supported on objects that have a DataBinding event" exception?

查看:17
本文介绍了为什么将 GridView 代码移动到 ascx 给定“数据绑定表达式仅在具有 DataBinding 事件的对象上受支持"?例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中创建了一个 GridView,并使用 DataTable 作为其数据源,效果很好.我现在已将 GridView 创建移动到 .ascx 文件中,以便于格式化.

I created a GridView in code with a DataTable as its data source which worked fine. I have now moved the GridView creation into a .ascx file to make it easier to format.

由于某种原因,这已经开始触发 HttpParseException:

For some reason this has started triggering the HttpParseException:

数据绑定表达式仅支持具有数据绑定事件.System.Web.UI.WebControls.HyperLinkField没有 DataBinding 事件.

Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.HyperLinkField does not have a DataBinding event.

造成这种差异的原因是什么?如何解决?

What's changed to cause this difference and how do I fix it?

原始代码:

// Set up columns for datagrid
var boundField = new HyperLinkField
                    {
                        HeaderText = "Title",
                        DataTextField = MembershipCollection.WebTitleColumnName,
                        DataNavigateUrlFields = new[] { MembershipCollection.WebUrlColumnName },
                        DataNavigateUrlFormatString = "{0}"
                    };

// Set up datagrid
_sitesList = new GridView
                {
                    AutoGenerateColumns = false,
                    AllowPaging = true,
                    PageSize = PageSize,
                    EmptyDataText = "No results",
                    Width = new Unit(100, UnitType.Percentage),
                    CellPadding = 2,
                    PagerSettings =
                        {
                            Visible = true,
                            Mode = PagerButtons.NextPrevious,
                            Position = PagerPosition.Bottom,
                            NextPageText = "Next >",
                            PreviousPageText = "< Previous"
                        }
                };
_sitesList.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
_sitesList.RowDataBound += GridView_RowDataBound;
_sitesList.PageIndexChanging += GridView_PageIndexChanging;
_sitesList.Columns.Add(boundField);

.ascx 代码:

<asp:ObjectDataSource ID="_sitesDataSource" runat="server"
    SelectMethod="GetSites"
    TypeName="System.Data.DataTable" />
<asp:GridView ID="_sitesGridView" runat="server"
    AutoGenerateColumns="false"
    AllowPaging="true"
    PageSize="<%# this.PageSize %>"
    EmptyDataText="No results"
    width="100%"
    DataSourceID="_sitesDataSource"
    OnRowDataBound="GridView_RowDataBound"
    OnPageIndexChanging="GridView_PageIndexChanging">
    <PagerSettings Visible="true" Mode="NextPrevious" Position="Bottom" NextPageText="Next >" PreviousPageText="< Previous" />
    <PagerStyle HorizontalAlign="Center" />
    <Columns>
        <asp:HyperlinkField
            HeaderText="Title"
            DataTextField="<%# MembershipCollection.WebTitleColumnName %>"
            DataNavigateUrlFields="<%# MembershipCollection.WebUrlColumnName %>"
            DataNavigateUrlFormatString="{0}" />
    </Columns>
</asp:GridView>

推荐答案

我刚刚意识到 - 我使用 <%# 而应该是 <%=在 HyperlinkField 声明中.

I just realised - I used <%# when it should have been <%= in the HyperlinkField declaration.

<%# 将尝试进行数据绑定,因为异常表明 HyperlinkField 没有关联的数据绑定.但是 <%= 只会执行代码(即从常量插入文本).

<%# is going to try and data bind, and as the exception indicates there is no associated data binding for the HyperlinkField. However <%= will simply execute code (i.e. insert the text from a constant).

这篇关于为什么将 GridView 代码移动到 ascx 给定“数据绑定表达式仅在具有 DataBinding 事件的对象上受支持"?例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆