<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>
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)); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)