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(); }
|