Why has moving GridView code to an ascx given an "Databinding expressions are only supported on objects that have a DataBinding event" exception?
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.
For some reason this has started triggering the HttpParseException:
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?
Original code:
// 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.Bot开发者_高级运维tom,
NextPageText = "Next >",
PreviousPageText = "< Previous"
}
};
_sitesList.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
_sitesList.RowDataBound += GridView_RowDataBound;
_sitesList.PageIndexChanging += GridView_PageIndexChanging;
_sitesList.Columns.Add(boundField);
.ascx code:
<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>
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).
精彩评论