Problem adding Category DropDownList to SimpleSearch Control
Posted: Tue Aug 18, 2009 4:46 pm
I need to be able to pass the selected category in the querystring to the Search page when the Search button from the SimpleSearch control is clicked. Below is the code from the code page and codebehind of my SimpleSeach control:
CodeBehind ( Categories are bound to the DDL in the InitializeCategoryTree event within this control):
protected void SearchButton_Click(object sender, EventArgs e)
{
string safeSearchPhrase = StringHelper.StripHtml(TxbSearchPhrase.Text);
//CPP - Added categoryID so it could be passed to search page
string categoryID = DdlCategories.SelectedValue;
if (!string.IsNullOrEmpty(safeSearchPhrase))
Response.Redirect("~/Search.aspx?k=" + Server.UrlEncode(safeSearchPhrase) + "&c=" + categoryID);
Response.Redirect("~/Search.aspx");
}
Code Page:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SimpleSearch.ascx.cs"
Inherits="ConLib_SimpleSearch" EnableViewState="false" %>
<%--
<conlib>
<summary>Displays a simple search box where search keywords can be entered, for results the customer will be redirected to search page.</summary>
</conlib>
--%>
Look For
<asp:TextBox ID="TxbSearchPhrase" runat="server" CssClass="searchPhrase"></asp:TextBox>
In
<asp:DropDownList ID="DdlCategories" runat="server" EnableViewState="true" CssClass="searchCategoriesList"
AppendDataBoundItems="True" DataTextField="Name"
DataValueField="CategoryId" AutoPostBack="True"
ondatabinding="DdlCategories_DataBinding"
onselectedindexchanged="DdlCategories_SelectedIndexChanged">
<asp:ListItem Text="- Any Category -" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="BtnSearchButton" UseSubmitBehavior="true" runat="server" Text="Search" OnClick="SearchButton_Click"
CssClass="searchButton" SkinID="ignore" CausesValidation="false" />
<asp:HyperLink ID="HplAdvancedSearch" NavigateUrl="~/AdvancedSearch.aspx" CssClass="advancedSearchLink"
runat="server">Advanced Search</asp:HyperLink>
CodeBehind ( Categories are bound to the DDL in the InitializeCategoryTree event within this control):
protected void SearchButton_Click(object sender, EventArgs e)
{
string safeSearchPhrase = StringHelper.StripHtml(TxbSearchPhrase.Text);
//CPP - Added categoryID so it could be passed to search page
string categoryID = DdlCategories.SelectedValue;
if (!string.IsNullOrEmpty(safeSearchPhrase))
Response.Redirect("~/Search.aspx?k=" + Server.UrlEncode(safeSearchPhrase) + "&c=" + categoryID);
Response.Redirect("~/Search.aspx");
}
Code Page:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SimpleSearch.ascx.cs"
Inherits="ConLib_SimpleSearch" EnableViewState="false" %>
<%--
<conlib>
<summary>Displays a simple search box where search keywords can be entered, for results the customer will be redirected to search page.</summary>
</conlib>
--%>
Look For
<asp:TextBox ID="TxbSearchPhrase" runat="server" CssClass="searchPhrase"></asp:TextBox>
In
<asp:DropDownList ID="DdlCategories" runat="server" EnableViewState="true" CssClass="searchCategoriesList"
AppendDataBoundItems="True" DataTextField="Name"
DataValueField="CategoryId" AutoPostBack="True"
ondatabinding="DdlCategories_DataBinding"
onselectedindexchanged="DdlCategories_SelectedIndexChanged">
<asp:ListItem Text="- Any Category -" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="BtnSearchButton" UseSubmitBehavior="true" runat="server" Text="Search" OnClick="SearchButton_Click"
CssClass="searchButton" SkinID="ignore" CausesValidation="false" />
<asp:HyperLink ID="HplAdvancedSearch" NavigateUrl="~/AdvancedSearch.aspx" CssClass="advancedSearchLink"
runat="server">Advanced Search</asp:HyperLink>