将 QueryExtender 与 EntityDataSource 结合使用时出错 - “FirstName”不是“System.Data.Common.DbDatRecord”类型的成员;
下面的 GridView 带有一个 EntityDataSource,可以从 Surveyors 表(包含更多字段)中获取 3 个字段,但当然它会向我显示每个 Surveyor。
<asp:GridView ID="gvwSurveyors" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" DataSourceID="edsSurveyors">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" ReadOnly="True" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" ReadOnly="True" SortExpression="LastName" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
</Columns>
</asp:GridView>
<asp:EntityDataSource ID="edsSurveyors" runat="server"
ConnectionString="name=PLSOEntities"
DefaultContainerName="PLSOEntities"
EnableFlattening="False"
EntitySetName="Surveyors"
Select="it.[FirstName], it.[LastName], it.[ID]"
AutoGenerateOrderByClause="true">
<OrderByParameters>
<asp:Parameter DefaultValue="LastName" />
</OrderByParameters>
</asp:EntityDataSource>
因此,为了使初始页面加载不显示所有内容,我将 OnSelecting 事件添加到 EntityDataSource 中,并让它在不是回发时取消查询。
protected void edsSurveyors_Selecting(object sender, EntityDataSourceSelectingEventArgs e) {
if (!Page.IsPostBack)
e.Cancel = true;
}
我有两个名为 txtFirstName 和 txtLastName 的文本框,我希望用户能够使用 SQL 查询 LIKE 样式进行搜索。网上的一些阅读让我转向了 QueryExtender。我将代码更改为以下内容:
<asp:GridView ID="gvwSurveyors" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" DataSourceID="edsSurveyors">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" ReadOnly="True" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" ReadOnly="True" SortExpression="LastName" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
</Columns>
</asp:GridView>
<asp:EntityDataSource ID="edsSurveyors" runat="server"
ConnectionString="name=PLSOEntities"
DefaultContainerName="PLSOEntities"
EnableFlattening="False"
EntitySetName="Surveyors"
Select="it.[FirstName], it.[LastName], it.[ID]"
AutoGenerateOrderByClause="true" onselecting="edsSurveyors_Selecting">
<OrderByParameters>
<asp:Parameter DefaultValue="LastName" />
</OrderByParameters>
</asp:EntityDataSource>
<asp:QueryExtender ID="qexSurveyor" runat="server" TargetControlID="edsSurveyors">
<asp:SearchExpression SearchType="Contains" DataFields="FirstName">
<asp:ControlParameter ControlID="txtFirstName" />
</asp:SearchExpression>
</asp:QueryExtender>
现在,当我单击按钮时,出现错误:“FirstName”不是“System.Data.Common.DbDataRecord”类型的成员
我该怎么做才能允许 Contains?一旦它起作用,我将添加一个具有相同功能的 LastName 参数。
The following GridView with an EntityDataSource that grabs 3 fields from the Surveyors table (which contains more fields) works, but of course it shows me every Surveyor.
<asp:GridView ID="gvwSurveyors" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" DataSourceID="edsSurveyors">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" ReadOnly="True" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" ReadOnly="True" SortExpression="LastName" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
</Columns>
</asp:GridView>
<asp:EntityDataSource ID="edsSurveyors" runat="server"
ConnectionString="name=PLSOEntities"
DefaultContainerName="PLSOEntities"
EnableFlattening="False"
EntitySetName="Surveyors"
Select="it.[FirstName], it.[LastName], it.[ID]"
AutoGenerateOrderByClause="true">
<OrderByParameters>
<asp:Parameter DefaultValue="LastName" />
</OrderByParameters>
</asp:EntityDataSource>
So in order to get the initial page load to not show everything I added the OnSelecting event to the EntityDataSource and have it cancel the query if it is Not a postback.
protected void edsSurveyors_Selecting(object sender, EntityDataSourceSelectingEventArgs e) {
if (!Page.IsPostBack)
e.Cancel = true;
}
I have two text boxes named txtFirstName and txtLastName that I want the user to be able to search using a SQL query LIKE style. Some reading on the web pointed me toward the QueryExtender. I changed the code to the following:
<asp:GridView ID="gvwSurveyors" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" DataSourceID="edsSurveyors">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" ReadOnly="True" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" ReadOnly="True" SortExpression="LastName" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
</Columns>
</asp:GridView>
<asp:EntityDataSource ID="edsSurveyors" runat="server"
ConnectionString="name=PLSOEntities"
DefaultContainerName="PLSOEntities"
EnableFlattening="False"
EntitySetName="Surveyors"
Select="it.[FirstName], it.[LastName], it.[ID]"
AutoGenerateOrderByClause="true" onselecting="edsSurveyors_Selecting">
<OrderByParameters>
<asp:Parameter DefaultValue="LastName" />
</OrderByParameters>
</asp:EntityDataSource>
<asp:QueryExtender ID="qexSurveyor" runat="server" TargetControlID="edsSurveyors">
<asp:SearchExpression SearchType="Contains" DataFields="FirstName">
<asp:ControlParameter ControlID="txtFirstName" />
</asp:SearchExpression>
</asp:QueryExtender>
So now when I click the button I get the error: 'FirstName' is not a member of type 'System.Data.Common.DbDataRecord'
What can I do to allow a Contains? Once it works, I will then added a LastName parameter that does the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我使用 AdventureWorks 作为数据源时,我也遇到了同样的事情。我必须从我的 EntityDataSource 中删除“Select=”。作为解决方法,我转到 GridView 并设置 AutoGenerateColumns="False"。然后,我手动添加了我想要在 Gridview 中显示的每一列,例如,
作为补充说明,似乎另一种解决方法(尽管是以编程方式)是使用 onquerycreated 事件将 LINQ 查询应用到 EntityDataSource。在这种情况下,您不需要以声明方式使用查询扩展器。完整文章在这里 -> http://msdn.microsoft.com/en-us/library/ee404748.aspx
I also ran into the same thing when using AdventureWorks as a data source. I had to remove the 'Select=' from my EntityDataSource. For a workaround, I went to my GridView and set AutoGenerateColumns="False". I then manually added each column I wanted to display in the Gridview, e.g.
As an additional note, it seems another workaround, albeit programatically, is to apply a LINQ Query to the EntityDataSource using the onquerycreated event. In this case, you don't need to declaratively use the Query Extender supposedly. Full Article Here -> http://msdn.microsoft.com/en-us/library/ee404748.aspx
我目前遇到了同样的问题,到目前为止,我发现如果我从 EntityDataSource 中删除“Select=”,它就可以工作。显然,使用 QueryExtender 时您被迫选择全部。但是,我不想返回表中的每个字段,而且我还没有找到解决此问题的方法。
那么,有人有解决方法吗?
I'm currently running into the same issue, I have found so far that if I remove the 'Select=' from the EntityDataSource it works. Apperently you are forced to select all when using the QueryExtender. However, I don't want to return every field in the table and I haven't found a way around this yet.
So, does anybody have a workaround?