Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an existing search page that will search records in a SQL DB using a stored procedure. Search results are posted to a GridView on the same page. I would like to include sort functionality because a search can return multiple records, but I'm not sure how to add this to the existing C#. The code for the GridView (as well as text box and search button) is currently:
ASP.NET
        <p>Search for someone by name, email address, or employee ID</p>
<!-- Panel sets Button1 as default, to click on Enter key -->    
    <asp:Panel ID="panSearch" runat="server" DefaultButton="Button1">
    <asp:TextBox ID="TextBox1" runat="server" MaxLength="100"></asp:TextBox>
    </asp:Panel>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" /><br />
    <br />

<!-- redirect to new URL on Select achieved through asp:hyperlinkfield -->
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="90%" AlternatingRowStyle-BackColor="#E2E2E2">
        <Columns>
            <asp:HyperLinkField DataNavigateUrlFields="PK" DataNavigateUrlFormatString="PeopleSearch2.aspx?PK={0}" Text="Select"></asp:HyperLinkField>
            <asp:BoundField DataField="FirstName" HeaderText="First" SortExpression="FirstName"></asp:BoundField>
            <asp:BoundField DataField="LastName" HeaderText="Last" SortExpression="LastName"></asp:BoundField>
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email"></asp:BoundField>
            <asp:BoundField DataField="Manager" HeaderText="Manager" SortExpression="Manager"></asp:BoundField>
            <asp:BoundField DataField="EmpStatus" HeaderText="Status" SortExpression="EmpStatus"></asp:BoundField>
                 

        </Columns>
    </asp:GridView>


My guess is that for the GridView, I would only need to add AllowSorting="true" and OnSorting="GridView1_Sorting" but then I'll need to add GridView1_Sorting to the code behind and possibly modify my existing C#. I don't want to lose any current functionality. The C# code is currently:

C#
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace Max.Security
{
    public partial class PeopleSearch1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Focus();  // this sets the cursor to the textbox on page load
            {
                if (Request.QueryString["searchString"] != null)
                {
                    DisplaySearchResults(Request.QueryString["searchString"]);
                }
            }
        }

        public void DisplaySearchResults(string strSearch)
        {
            SqlCommand cmd = new SqlCommand("dbo.aspPeopleSearch", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@SearchString", strSearch);
            cmd.Connection.Open();

            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();

            cmd.Connection.Close();
            cmd.Connection.Dispose();
        }


        protected void Button1_Click(object sender, EventArgs e)  // this allows the Enter key to fire button1
        {
            Response.Redirect("PeopleSearch1.aspx?searchString=" + Server.UrlEncode(TextBox1.Text)); 
        }



    }
}
Posted
Comments
syed shanu 2-Dec-14 19:52pm    
Check this link how this will help you.
http://www.codeproject.com/Tips/663532/How-to-Perform-Sorting-in-Gridview-in-ASP-NET

http://www.dotnetfox.com/articles/gridview-sorting-example-in-Asp-Net-using-C-Sharp-1082.aspx
http://www.dotnetlearners.com/blogs/view/69/Aspnet-gridview-sorting-example.aspx
eleventy7 8-Dec-14 15:47pm    
Thank you for the links. I'm able to create a new page with sorting based on the information found in those tutorials. However, I'm new to C#, and I still don't understand how to modify my existing code to include sorting. My existing page performs a search using a stored procedure in SQL, then prints the results of the search. To add sorting on the search results, can I do this within my existing "DisplaySearchResults" or do I need to add a new block of code for the OnSorting?
syed shanu 8-Dec-14 19:09pm    
Hi,
I recomand you to create a new website and create similar search functionality using the above links .once you get familiar with c# and how to work with search and sorting ,Then you can easily change your existing code.All the best.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900