Solved

Adding a Search textbox to a GridView

Posted on 2013-10-31
4
1,695 Views
Last Modified: 2013-11-01
I need to add a Search feature to a GridView using a textbox and button. I am using a ObjectDataSource as seen in my code. The parameters are set but I am not sure how to filter it without create a new Select Method call that allows the new parameter.

I have been looking at options like the ObjectDataSource.FilterParameters Property but I get the error
The data source 'dsAssocObject' only supports filtering when the SelectMethod returns a DataSet or a DataTable.
Could you explain to me what this error means and a possible solution. I cannot change the original SelectMethod="GetEmployeeList" or its parameters. So is it possible to do this dynamically either directly on the aspx page or the code behind page?

I really need this solution like today (10/30), tomorrow the lastest. Please anyone I need you help. If I could give out more points due to the urgency of this solution I would. But I will have several more questions following this especially concerning the filtering option again with a checkbox. So stay tuned... And thanks!

                        <asp:Button runat="server" ID="btnInsertEmployeeDetails" CssClass="CssEditEmployeeAdminPanel" Text="Create New Employee" Width="150px" OnClick="btnInsertEmployeeDetails_Click"/>
                        <asp:Label cssclass="CurvedTitleVED" ID="lblTitleVED" runat="server" Text="Select Employee"/>
                        <asp:CheckBox ID="chkShowInactiveRecords" runat="server" Text="Show Inactive Records" CssClass="CssChkBoxShowDisabled" OnCheckedChanged="chkShowInactiveRecords_CheckedChanged" Width="200px" AutoPostBack="true" Checked="True" />
                        <asp:GridView ID="grdEmpAdmin" OnRowCommand="grdEmpAdmin_RowCommand" runat="server" AutoGenerateColumns="False" DataSourceID="dsEmpObject" AllowPaging="True" PageSize="15" CssClass="CssZMGrid" CellPadding="3" GridLines="None" AllowSorting="false" BackColor="White" BorderColor="AliceBlue" BorderStyle="None" BorderWidth="0" CellSpacing="1" >
                            <Columns>
                                <asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkSelectEmployee" runat="server" CommandName="cmdEmpSelect" CommandArgument='<%#Eval("EmployeeID")%>'>
                                            <asp:Image ID="imgSelectEmployee" runat="server" ImageUrl="~/images/MaintenanceGrid.png" BorderStyle="None" width="20" height="20"/>
                                        </asp:LinkButton>
                                        <asp:Image ID="imgRunning" runat="server" ImageUrl="~/images/running.gif" BorderStyle="None" width="20" height="20" Visible="false"/>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Full Name" SortExpression="FullName">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtFullName" runat="server" Text='<%# Bind("FullName") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblFullName" runat="server" Text='<%# Bind("FullName") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Employee Number" SortExpression="EmployeeNumber">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtEmployeeNumber" runat="server" Text='<%# Bind("EmployeeNumber") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblEmployeeNumber" runat="server" Text='<%# Bind("EmployeeNumber") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Department Name" SortExpression="DepartmentName">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtDepartmentName" runat="server" Text='<%# Bind("DepartmentName") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Bind("DepartmentName") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Job Title Description" SortExpression="JobTitleDescription">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtJobTitleDescription" runat="server" Text='<%# Bind("JobTitleDescription") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblJobTitleDescription" runat="server" Text='<%# Bind("JobTitleDescription") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <RowStyle BackColor="#EFF3FB" />
			                <AlternatingRowStyle BackColor="White" />
                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#000000" />
                            <EditRowStyle BackColor="#2461BF" />
                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <SortedAscendingCellStyle BackColor="#F5F7FB" />
                            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                            <SortedDescendingCellStyle BackColor="#E9EBEF" />
                            <SortedDescendingHeaderStyle BackColor="#4870BE" />
                        </asp:GridView>
                        <asp:ObjectDataSource ID="dsEmpObject" runat="server" SelectMethod="GetEmployeeList" TypeName="Admin.Workshop.EmployeeWorkshop" FilterExpression="FullName='{0}'" OnFiltering="dsEmpObject_Filtering">
                            <SelectParameters>
                                <asp:Parameter DefaultValue="" Direction="Output" Name="strReturnMessage" Type="String" />
                                <asp:Parameter DefaultValue="1" Name="tLocationID" Type="Int32" />
                                <asp:Parameter DefaultValue="true" Name="blDisabled" Type="Boolean" />
                            </SelectParameters>
                            <FilterParameters>
                                <asp:FormParameter Name="FullName" FormField="txtSearch"/>
                            </FilterParameters>
                        </asp:ObjectDataSource>

Open in new window

0
Comment
Question by:RedXavier2k4
  • 2
  • 2
4 Comments
 

Author Comment

by:RedXavier2k4
ID: 39614215
This is my GetEmployeeList Code. Can I use LINQ or something to return this code into a List, or dataset, datatable?
        public List<Admin.Employee> GetcList(out string strReturnMessage, int tSiteID, bool blDisabled)
        {
            List<Admin.Employee> EmployeeAdminList = new List<Admin.vwEmployee>();

            try
            {
                EmployeeAdminList = mywebservice.GetEmployees(out strReturnMessage, tLocationID, blDisabled);
            }
            catch (Exception ex)
            {
                strReturnMessage = ex.Message.ToString();
            }
            return EmployeeAdminList;
        }

Open in new window

0
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 39614282
I don't know what your search parameters are since you are using stored proc but assuming they are firstname and lastname, then add thes to the <SelectParameters>...</SELECTParameters?

                                     <asp:ControlParameter ControlID="txtSearch" Name="firstName" PropertyName="Text" Type="String" />
                                     <asp:ControlParameter ControlID="txtSearch" Name="lastName" PropertyName="Text" Type="String" />

Open in new window


Then at top of your gridview, NOT inside it, add these

        <asp:TextBox ID="txtSearch" runat="server" style="height:23px"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server" TabIndex="1" height="27px" Text="GO" OnClick="btnSearch_Click" class="btn" />

Open in new window


Finally, on codebehind, add this sub:

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs)
        GridView1.DataBind()
    End Sub

Open in new window


All things being equal, this should work
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 39614287
sorry, i didn't know you are using linq
0
 

Author Comment

by:RedXavier2k4
ID: 39614344
Thanks for your reply. Yes I am using LINQ. I am pretty new to LINQ.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now