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>
Problem adding Category DropDownList to SimpleSearch Control
Re: Problem adding Category DropDownList to SimpleSearch Control
For this you will need to put a dropdown by populating categories in it and finally build search page URL with category parameter against button click to redirect user. Here is modified SimpleSearch control files, download and try these files
Re: Problem adding Category DropDownList to SimpleSearch Control
I wasn't able to get this working successfully. It actually does the same thing that the code I created does.
A few problems I'm seeing is that the categories will load into the DDL if not PostBack, but when a PostBack
occurs it loses it's state and all values bound to it. I've tried accessing the SelectedValue of the DDL in various
events, but it's never available. Viewstate doesn't seem to be saving / loading for this control. I manually passed the value of a category and search works fine, just need to pass it programmatically from the DDL.
This is becoming a holdup, hope the problem is found. May have to use Javascript to store value in a client-side field and access it in the click event of the Search button????
A few problems I'm seeing is that the categories will load into the DDL if not PostBack, but when a PostBack
occurs it loses it's state and all values bound to it. I've tried accessing the SelectedValue of the DDL in various
events, but it's never available. Viewstate doesn't seem to be saving / loading for this control. I manually passed the value of a category and search works fine, just need to pass it programmatically from the DDL.
This is becoming a holdup, hope the problem is found. May have to use Javascript to store value in a client-side field and access it in the click event of the Search button????
- Logan Rhodehamel
- Developer
- Posts: 4116
- Joined: Wed Dec 10, 2003 5:26 pm
Re: Problem adding Category DropDownList to SimpleSearch Control
Code: Select all
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SimpleSearch.ascx.cs"
Inherits="ConLib_SimpleSearch" EnableViewState="false" %>
Cheers,
Logan
.com
If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.
Logan

If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.
Re: Problem adding Category DropDownList to SimpleSearch Control
This problem has been resolved. Viewstated had to be enabled on the page and the dropdown list control, then I was able to get the dropdown list values on postback.