以下带有 EntityDataSource 的 GridView 可以从 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' 类型的成员
我可以做些什么来允许包含?一旦它工作,我将添加一个相同的 LastName 参数。