Page 1 of 1

Problem adding Category DropDownList to SimpleSearch Control

Posted: Tue Aug 18, 2009 4:46 pm
by webmanWil
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&nbsp;
<asp:TextBox ID="TxbSearchPhrase" runat="server" CssClass="searchPhrase"></asp:TextBox>
&nbsp;In&nbsp;
<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" />
&nbsp;
<asp:HyperLink ID="HplAdvancedSearch" NavigateUrl="~/AdvancedSearch.aspx" CssClass="advancedSearchLink"
runat="server">Advanced Search</asp:HyperLink>

Re: Problem adding Category DropDownList to SimpleSearch Control

Posted: Mon Aug 31, 2009 8:30 pm
by mazhar
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

Posted: Tue Sep 01, 2009 6:23 pm
by webmanWil
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????

Re: Problem adding Category DropDownList to SimpleSearch Control

Posted: Sat Sep 19, 2009 12:04 pm
by Logan Rhodehamel

Code: Select all

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SimpleSearch.ascx.cs"
Inherits="ConLib_SimpleSearch" EnableViewState="false" %>
EnableViewState="false" disables viewstate for the whole control.

Re: Problem adding Category DropDownList to SimpleSearch Control

Posted: Sat Oct 24, 2009 7:19 pm
by webmanWil
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.