为什么将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?
问题描述
我创建了一个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:
数据绑定表达式仅对
DataBinding事件的对象支持
。
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.
<%#
将尝试和数据绑定,并且由于异常表示没有关联的超链接数据绑定。然而, <%=
只会执行代码(即插入常量的文本)。
<%#
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屋!