views:

41

answers:

1

Hi, I am using EntityDataSource control to display Data in Grid, I want to use the same EntityDataSource to display all data as well as per the search, the problem is I can do only one thing either can use it to search or to get whole data

here is my .aspx page

<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>

Thanks

A: 

I setup a page using the Northwind DB and using the Employees table. There is a TextBox control and Button controls just like your code to filter the data. When the TextBox is empty, all records are displayed. When a EmployeeID is entered into the TextBox, and the 'Filter' button is clicked, only the employee with the EmployeeID is displayed(if present in the data). I think this is the kind of behavior you are asking for. Here is the markup:

<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>

Edit: The question has been raised as how part of a value could be entered into the filter text box to get all the values that start with the entered value. You could use the LIKE SQL operator and the users would need to be told that the "%" character is the wildcard. I changed the filter to the FirstName field. Here are the changes to the above code:

In the EntityDataSource tag:

AutoGenerateWhereClause="false"

Where="it.[FirstName] LIKE @FirstName"

In the WhereParameters tag:

<asp:FormParameter FormField="txtFilter" Name="FirstName" Type="String" DefaultValue="%" />

Now, when "n%" is entered into the filter textbox, all records with a FirstName value that starts with "n" will be displayed. Any valid LIKE clause could now be used.

DaveB
Thanks DaveB, its working, but its search only for full text for example: if the Employee name is "David" and I input text "Dav" for search it not return any result..
BreakHead
@Qutbuddin Kamaal - See the additions to my answer.
DaveB