title: Using Dropdown List for options for Search function in ASP.NET
body: <p>I did work on a Search function where the user can type the item they want to search on a textbox, then hit a button to search.
  the Search function is already working but for one option only (ContractNo), now I like to add a Dropdown list to add more options for the users to search (add: EmpID, TrainingCode etc)</p>
  
  <blockquote>
    <p>here is the code for the aspx: (yes i only just included the codes that I think is necessary)</p>
  </blockquote>
  
  <pre><code> &lt;div id="contentarea"&gt;
              &lt;p&gt; Search Employee ID, Training Code, Contract Number&lt;br/&gt;
                  &lt;asp:DropDownList ID="DropDownList1" runat="server"&gt;
                      &lt;asp:ListItem&gt;Contract Number&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Training Code&lt;/asp:ListItem&gt;
                      &lt;asp:ListItem&gt;Employee ID&lt;/asp:ListItem&gt;
                  &lt;/asp:DropDownList&gt;
                  &lt;asp:TextBox ID="searchText" runat="server" Height="16px" Width="146px"&gt;&lt;/asp:TextBox&gt;
                  &lt;asp:Button ID="ButtonSearch" runat="server" Text="Search" OnClick="ButtonSearch_Click" /&gt;
                  &lt;asp:Button ID="ButtonClear" runat="server" Text="Clear" OnClick="ButtonClear_Click" /&gt;
              &lt;/p&gt;
              &lt;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"&gt;
                  &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
                  &lt;Columns&gt;
                     &lt;/a&gt;
                          &lt;/Itemtemplate&gt;
                      &lt;/asp:TemplateField&gt;--%&gt;
                      &lt;asp:TemplateField HeaderText="ContractNo" SortExpression="ContractNo"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="LabelContractNo" Text='&lt;% #HighlightText(Eval("ContractNo").ToString()) %&gt;' runat="server"&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="TrainingCode" SortExpression="TrainingCode"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="LabelTrainingCode" runat="server" Text='&lt;%# Eval("TrainingCode") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="EmpID" SortExpression="EmpID"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="LabelEmpID" runat="server" Text='&lt;%# Eval("EmpID") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="ContractDate" SortExpression="ContractDate"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="LabelContractDate" runat="server" Text='&lt;%# Eval("ContractDate") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="ContractDuration" SortExpression="ContractDuration"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="LabelContractDuration" runat="server" Text='&lt;%# Eval("ContractDuration") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="ServiceStart" SortExpression="ServiceStart"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="LabelServiceStart" runat="server" Text='&lt;%# Eval("ServiceStart") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="ContractValue" SortExpression="ContractValue"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="LabelContractValue" runat="server" Text='&lt;%# Eval("ContractValue") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:TemplateField HeaderText="Served" SortExpression="Served"&gt;
                          &lt;ItemTemplate&gt;
                              &lt;asp:Label ID="LabelServed" runat="server" Text='&lt;%# Bind("Served") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                  &lt;/Columns&gt;
  
              &lt;/asp:GridView&gt;
              &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:dbx %&gt;" 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}%'"&gt;
  
                  &lt;FilterParameters&gt;
                      &lt;asp:ControlParameter Name="ContractNo" ControlID="searchText" PropertyName="Text"/&gt;
                  &lt;/FilterParameters&gt; 
  &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <blockquote>
    <p>and the FULL C# backend code:</p>
  </blockquote>
  
  <pre><code>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 ("&lt;span class=highlight&gt;" + m.Value + "&lt;/span&gt;");
  }
  
  
  protected void ButtonSearch_Click(object sender, EventArgs e)
  {
      SearchString = searchText.Text;
  }
  
  protected void ButtonClear_Click(object sender, EventArgs e)
  {
      searchText.Text = "";
      SearchString = "";
      GridView1.DataBind();
  
  }
  </code></pre>
  
  <p>Now im thinking to add an if statement maybe? on the FilterParameters? like </p>
  
  <pre><code>&lt;% if (DropDownList1.text == "EmpID"){}
  </code></pre>
  
  <p>but Im not sure.. Can you suggest other methods please?? Let me know if you might need other codes from the program. Thank you in advance</p>