C# 在ASP.NET中使用搜索功能选项的下拉列表

C# 在ASP.NET中使用搜索功能选项的下拉列表,c#,asp.net,gridview,webforms,C#,Asp.net,Gridview,Webforms,我开发了一个搜索功能,用户可以在文本框中键入他们想要搜索的项目,然后点击按钮进行搜索。 搜索功能已经开始工作,但只针对一个选项(ContractNo),现在我想添加一个下拉列表,为用户添加更多的搜索选项(添加:EmpID、TrainingCode等) 以下是aspx的代码:(是的,我只包括了我认为必要的代码) 搜索员工ID、培训代码、合同号 合同号 培训代码 员工ID --%> 以及完整的C#后端代码: 私有字符串SearchString=”“; 受保护的无效页面加载(对象发送方、事件参数

我开发了一个搜索功能,用户可以在文本框中键入他们想要搜索的项目,然后点击按钮进行搜索。 搜索功能已经开始工作,但只针对一个选项(ContractNo),现在我想添加一个下拉列表,为用户添加更多的搜索选项(添加:EmpID、TrainingCode等)

以下是aspx的代码:(是的,我只包括了我认为必要的代码)


搜索员工ID、培训代码、合同号
合同号 培训代码 员工ID

--%>
以及完整的C#后端代码:

私有字符串SearchString=”“;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
公共字符串高亮文本(字符串输入文本)
{
字符串搜索\u str=searchText.Text;
Regex RegExp=new Regex(Search_str.Replace(“,“|”).Trim(),RegexOptions.IgnoreCase);
返回RegExp.Replace(InputText,newMatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(Match m)//这只是为了突出显示搜索的项目
{
返回(“+m.值+”);
}
受保护的无效按钮搜索单击(对象发送者,事件参数e)
{
SearchString=searchText.Text;
}
受保护的无效按钮清除单击(对象发送者,事件参数e)
{
searchText.Text=”“;
SearchString=“”;
GridView1.DataBind();
}
现在我想添加一个if语句,也许?关于过滤器参数?像

<% if (DropDownList1.text == "EmpID"){}

尝试使用通用选项。使用下拉列表列出所有类型(EmpID、培训代码)。根据类型选择,使用开关根据文本框中输入的文本进行过滤

SearchString=searchText.Text; 开关(类型) { 案件编号: DoOperation(); 打破 案例培训代码: DoOperation(); 打破
}尝试使用通用选项。使用下拉列表列出所有类型(EmpID、培训代码)。根据类型选择,使用开关根据文本框中输入的文本进行过滤

SearchString=searchText.Text; 开关(类型) { 案件编号: 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>

搜索员工ID、培训代码、合同号
合同号 培训代码 员工ID

这是aspx.cs中的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>");
    }

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

    }
私有字符串SearchString=”“;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
公共字符串高亮文本(字符串输入文本)
{
字符串搜索\u str=searchText.Text;
Regex RegExp=new Regex(Search_str.Replace(“,“|”).Trim(),RegexOptions.IgnoreCase);
返回RegExp.Replace(InputText,newMatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(Match m)//这只是为了突出显示搜索的项目
{
返回(“+m.值+”);
}
私有数据表GetData(字符串查询)
{
//从web.config文件读取连接字符串,重要信息,请更改
//此处的ConnectionString名称(testConnectionString)
//并将其替换为您的连接名,您可以在web.config文件中找到它

//在tag中,你可以在这个标签后找到它好的,这里是解决方案,它在我的计算机中正常工作,我希望这就是你想要实现的。我在代码中添加了一些注释,阅读它们

在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>

搜索员工ID、培训代码、合同号
合同号 培训代码 员工ID

这是aspx.cs中的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>");
    }

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

    }
私有字符串SearchString=”“;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
公共字符串高亮文本(字符串输入文本)
{
字符串搜索\u str=searchText.Text;
Regex RegExp=new Regex(Search_str.Replace(“,“|”).Trim(),RegexOptions.IgnoreCase);
返回RegExp.Replace(InputText,newMatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(Match m)//这只是为了突出显示搜索的项目
{
返回(“+m.值+”);
}
私有数据表GetData(字符串查询)
{
//从web.config文件读取连接字符串,重要信息