使用下拉列表作为 ASP.NET 中搜索功能的选项
本文关键字:搜索 功能 选项 NET ASP 下拉列表 | 更新日期: 2023-09-27 17:55:42
我确实开发了一个搜索功能,用户可以在文本框中键入他们想要搜索的项目,然后点击一个按钮进行搜索。搜索功能已经工作,但仅适用于一个选项(ContractNo),现在我想添加一个下拉列表以添加更多选项供用户搜索(添加:EmpID,培训代码等)
这是ASPX的代码:(是的,我只是包含了我认为必要的代码)
<div id="contentarea">
<p> Search Employee ID, Training Code, Contract Number<br/>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Contract Number</asp:ListItem>
<asp:ListItem>Training Code</asp:ListItem>
<asp:ListItem>Employee ID</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="searchText" runat="server" Height="16px" Width="146px"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" Text="Search" OnClick="ButtonSearch_Click" />
<asp:Button ID="ButtonClear" runat="server" Text="Clear" OnClick="ButtonClear_Click" />
</p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="TrainingCode" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display." ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
</a>
</Itemtemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="ContractNo" SortExpression="ContractNo">
<ItemTemplate>
<asp:Label ID="LabelContractNo" Text='<% #HighlightText(Eval("ContractNo").ToString()) %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TrainingCode" SortExpression="TrainingCode">
<ItemTemplate>
<asp:Label ID="LabelTrainingCode" runat="server" Text='<%# Eval("TrainingCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmpID" SortExpression="EmpID">
<ItemTemplate>
<asp:Label ID="LabelEmpID" runat="server" Text='<%# Eval("EmpID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ContractDate" SortExpression="ContractDate">
<ItemTemplate>
<asp:Label ID="LabelContractDate" runat="server" Text='<%# Eval("ContractDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ContractDuration" SortExpression="ContractDuration">
<ItemTemplate>
<asp:Label ID="LabelContractDuration" runat="server" Text='<%# Eval("ContractDuration") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ServiceStart" SortExpression="ServiceStart">
<ItemTemplate>
<asp:Label ID="LabelServiceStart" runat="server" Text='<%# Eval("ServiceStart") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ContractValue" SortExpression="ContractValue">
<ItemTemplate>
<asp:Label ID="LabelContractValue" runat="server" Text='<%# Eval("ContractValue") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Served" SortExpression="Served">
<ItemTemplate>
<asp:Label ID="LabelServed" runat="server" Text='<%# Bind("Served") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbx %>" DeleteCommand="DELETE FROM [SCHOLARSHIPCONTRACT] WHERE [TrainingCode] = @TrainingCode" InsertCommand="INSERT INTO [SCHOLARSHIPCONTRACT] ([ContractNo], [TrainingCode], [EmpID], [ContractDate], [ContractDuration], [ServiceStart], [ContractValue], [Served]) VALUES (@ContractNo, @TrainingCode, @EmpID, @ContractDate, @ContractDuration, @ServiceStart, @ContractValue, @Served)" SelectCommand="SELECT * FROM [SCHOLARSHIPCONTRACT]" UpdateCommand="UPDATE [SCHOLARSHIPCONTRACT] SET [ContractNo] = @ContractNo, [EmpID] = @EmpID, [ContractDate] = @ContractDate, [ContractDuration] = @ContractDuration, [ServiceStart] = @ServiceStart, [ContractValue] = @ContractValue, [Served] = @Served WHERE [TrainingCode] = @TrainingCode" FilterExpression="ContractNo '%{0}%'">
<FilterParameters>
<asp:ControlParameter Name="ContractNo" ControlID="searchText" PropertyName="Text"/>
</FilterParameters>
</asp:SqlDataSource>
和完整的 C# 后端代码:
private string SearchString = "";
protected void Page_Load(object sender, EventArgs e)
{
}
public string HighlightText(string InputText)
{
string Search_str = searchText.Text;
Regex RegExp = new Regex(Search_str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
return RegExp.Replace(InputText, new MatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(Match m) //this is just to highlight the item searched
{
return ("<span class=highlight>" + m.Value + "</span>");
}
protected void ButtonSearch_Click(object sender, EventArgs e)
{
SearchString = searchText.Text;
}
protected void ButtonClear_Click(object sender, EventArgs e)
{
searchText.Text = "";
SearchString = "";
GridView1.DataBind();
}
现在我想添加一个 if 语句也许? 在过滤器参数上?
<% if (DropDownList1.text == "EmpID"){}
但我不确定..你能建议其他方法吗?让我知道您是否可能需要该程序中的其他代码。提前谢谢你
尝试使用泛型选项。使用下拉列表列出所有类型(EmpID、TrainingCode)。在类型选择的基础上,使用"切换"按文本框中输入的文本进行筛选。
SearchString = searchText.Text;开关(类型){ 案例 EmpID: DoOperation(); 破; 案例培训代码: DoOperation(); 破;}
好的
,这是解决方案,它在我的计算机中工作正常,我希望这就是您想要实现的目标。我在代码中放了一些注释,阅读了它们
在ASPX页中,把这段代码放进
去<div id="contentarea">
<p> Search Employee ID, Training Code, Contract Number<br/>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="ContractNo">Contract Number</asp:ListItem>
<asp:ListItem Value="TrainingCode">Training Code</asp:ListItem>
<asp:ListItem Value="EmpID">Employee ID</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="searchText" runat="server" Height="16px" Width="146px"></asp:TextBox>
<asp:Button ID="ButtonSearch" runat="server" Text="Search" OnClick="ButtonSearch_Click" />
<asp:Button ID="ButtonClear" runat="server" Text="Clear" OnClick="ButtonClear_Click" />
</p>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
这是 aspx 中的 C# 代码.cs
private string SearchString = "";
protected void Page_Load(object sender, EventArgs e)
{
}
public string HighlightText(string InputText)
{
string Search_str = searchText.Text;
Regex RegExp = new Regex(Search_str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
return RegExp.Replace(InputText, new MatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(Match m) //this is just to highlight the item searched
{
return ("<span class=highlight>" + m.Value + "</span>");
}
private DataTable GetData(string query)
{
// Read connection string from web.config file , Important, change the
//ConnectionStrings name here (testConnectionString)
// and replace it with your connection name , you can find it in web.config file
// in <connectionStrings> tag , you find it after this tag <add name="
string CS = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlDataAdapter sda = new SqlDataAdapter(query, con);
using (DataTable dt = new DataTable())
{
con.Open();
sda.Fill(dt);
return dt;
}
}
}
protected void ButtonSearch_Click(object sender, EventArgs e)
{
SearchString = searchText.Text;
string columnName = DropDownList1.SelectedValue;
string searchSQL = "SELECT * FROM [SCHOLARSHIPCONTRACT] WHERE " + columnName + "= '" + SearchString + "'";
//bind the SCHOLARSHIPCONTRACT table data into GridView1
DataTable dt = this.GetData(searchSQL);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void ButtonClear_Click(object sender, EventArgs e)
{
searchText.Text = "";
SearchString = "";
GridView1.DataBind();
}