本文介绍了C#开发软件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从数据库中搜索员工并在网格上显示结果
how to search an employee from a database and display the results on a grid
推荐答案
<div>
Enter first name to search:
</div>
<div>
<asp:TextBox ID="txtSearch" runat="server" AutoPostBack="true" OnTextChanged="txtSearch_TextChanged">
</asp:TextBox>
</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>
.CS文件
.CS file
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();
}
这篇关于C#开发软件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!