我正在使用EntityDataSource控件在Grid中显示Data,我想使用相同的EntityDataSource来显示所有数据以及每次搜索,问题是我只能做一件事就是可以用它来搜索或获取整个数据
这是我的.aspx页面
<asp:EntityDataSource ID="InvestorsSource" runat="server"
ConnectionString="name=Entities"
DefaultContainerName="Entities" EnableFlattening="False"
EntitySetName="Investors" EntityTypeFilter="Investor"
Select="it.[InvestorId], it.[InvestorName], it.[Summary], it.[Logo], it.[EmailAddress], it.[PhoneNumber], it.[Website]"
AutoGenerateWhereClause="True" OrderBy="it.[InvestorName]">
<WhereParameters>
<asp:FormParameter FormField="txtSearchInvestor" Name="investorName" Type="String" />
</WhereParameters>
</asp:EntityDataSource>
<asp:TextBox ID="txtSearchInvestor" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />
<p>
<asp:GridView ID="gvInvestors" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="InvestorsSource">
<Columns>
<asp:BoundField DataField="InvestorId" HeaderText="InvestorId" ReadOnly="True"
SortExpression="InvestorId" />
<asp:BoundField DataField="InvestorName" HeaderText="InvestorName"
SortExpression="InvestorName" ReadOnly="True" />
<asp:BoundField DataField="Summary" HeaderText="Summary"
SortExpression="Summary" ReadOnly="True" />
<asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress"
SortExpression="EmailAddress" ReadOnly="True" />
<asp:BoundField DataField="PhoneNumber" HeaderText="PhoneNumber"
SortExpression="PhoneNumber" ReadOnly="True" />
<asp:BoundField DataField="Website" HeaderText="Website"
SortExpression="Website" ReadOnly="True" />
</Columns>
</asp:GridView>
</p>
由于
答案 0 :(得分:0)
我使用Northwind DB并使用Employees表设置页面。有一个TextBox控件和Button控件就像你的代码一样过滤数据。当TextBox为空时,将显示所有记录。将EmployeeID输入TextBox并单击“过滤器”按钮时,仅显示具有EmployeeID的员工(如果存在于数据中)。我认为这是你要求的那种行为。这是标记:
<form id="form1" runat="server">
<div>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=NWEntities" DefaultContainerName="NWEntities"
EntitySetName="EmployeeSet" EntityTypeFilter="Employee" AutoGenerateWhereClause="true"
Select="it.[EmployeeID], it.[LastName], it.[FirstName], it.[Title], it.[TitleOfCourtesy], it.[BirthDate]">
<WhereParameters>
<asp:FormParameter FormField="txtFilter" Name="EmployeeID" Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
<asp:TextBox ID="txtFilter" runat="server" />
<asp:Button runat="server" Text="Filter" />
<asp:GridView ID="gvEmployees" runat="server" DataSourceID="EntityDataSource1" AutoGenerateColumns="true" />
</div>
</form>
编辑:提出了一个问题,即如何将值的一部分输入到过滤器文本框中,以获取以输入值开头的所有值。您可以使用LIKE SQL运算符,并且需要告知用户“%”字符是通配符。我将过滤器更改为FirstName字段。以下是对上述代码的更改:
在EntityDataSource标记中:
AutoGenerateWhereClause="false"
Where="it.[FirstName] LIKE @FirstName"
在WhereParameters标签中:
<asp:FormParameter FormField="txtFilter" Name="FirstName" Type="String" DefaultValue="%" />
现在,当在过滤器文本框中输入“n%”时,将显示FirstName值以“n”开头的所有记录。现在可以使用任何有效的LIKE子句。