Search into Textbox and Bind Gridview using Asp.net



Design page for Gridview and textbox :

<div>
Enter first name to search:
</div>
<div>
<asp:TextBox ID="txtSearch" runat="server" AutoPostBack="true" OnTextChanged="txtSearch_TextChanged">
</asp:TextBox>&nbsp;
</div>
<div>
<asp:UpdatePanel ID="updatepanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="grdSearch" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Employee Id">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Highlight(Eval("emp_id").ToString()) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee First Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%#(Eval("emp_first_name")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Middle Name">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#(Eval("emp_middle_name")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Last Name">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#(Eval("emp_last_name")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#(Eval("emp_name")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtSearch" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</div>

Code for This page :

 string strConnection = "Data Source=BITPLUS3\\SqlExpress;Initial Catalog=HRDB;User ID=sa;pwd=bit123";

//ConfigurationManager.AppSettings["WebHRConnectionString"];

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
// BindGrid();
}

}

private DataTable GetRecords()
{
SqlConnection conn = new SqlConnection(strConnection);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select emp_id, emp_first_name, emp_middle_name,emp_last_name, emp_name from tblemployee";

SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
DataSet objDs = new DataSet();
dAdapter.Fill(objDs);
return objDs.Tables[0];

}
private void BindGrid()
{
DataTable dt = GetRecords();
if (dt.Rows.Count > 0)
{
grdSearch.DataSource = dt;
grdSearch.DataBind();
}
}
private void SearchText()
{
DataTable dt = GetRecords();
DataView dv = new DataView(dt);
string SearchExpression = null;
if (!String.IsNullOrEmpty(txtSearch.Text))
{
SearchExpression = string.Format("{0} '%{1}%'",
grdSearch.SortExpression, txtSearch.Text);

}

dv.RowFilter = "emp_name like" + SearchExpression;
grdSearch.DataSource = dv;
grdSearch.DataBind();

}
public string Highlight(string InputTxt)
{
string Search_Str = txtSearch.Text.ToString();
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(),
RegexOptions.IgnoreCase);

// Highlight keywords by calling the
//delegate each time a keyword is found.
return RegExp.Replace(InputTxt,
new MatchEvaluator(ReplaceKeyWords));

// Set the RegExp to null.
RegExp = null;

}

public string ReplaceKeyWords(Match m)
{

return "<span class=highlight>" + m.Value + "</span>";

}

protected void txtSearch_TextChanged(object sender, EventArgs e)
{

SearchText();
}

Ebook Download
View all
milton
Read by 0 people
Download Now!
Learn
View all