Currently, I have a text box and a button on my master page for entering and submitting a search string. The search is internal to my website. The user would enter the search string then click on the search button. The search button redirects to the search results page. When the search results page is loaded, it currently searches through the controls on the previous page to find the search string textbox to retrieve the search string.
This all works fine except when a search is entered from the log-in page due to an exception: The current user is not allowed to access the previous page.
So I'm wondering what might be a better way to retrieve the search string. I'd prefer not to clutter the redirect url with it. I'm not adverse to storing it in the Session collection but am having trouble figuring out how to get it there before the Page load event occurs.
Below is the pertinet code:
From the master page:
<asp:TextBox ID="SearchTextBox" runat="server" />
<asp:ImageButton ID="SearchButton" runat="server" Text="Search" PostBackUrl="~/Search.aspx" ImageUrl="~/Graphics/btn_search.png" ImageAlign="AbsMiddle" />
From the search results page:
private string getSearchTextBoxText(Control ctrl)
{
if (ctrl.HasControls())
{
foreach (Control child in ctrl.Controls)
{
string s = getSearchTextBoxText(child);
if (s != null)
return s;
}
}
else if (ctrl.ID == "SearchTextBox")
{
return ((TextBox)ctrl).Text;
}
return null;
}
protected void Page_Load(object sender, EventArgs e)
{
log.Debug("Entering " + MethodBase.GetCurrentMethod().Name);
string lastSearchText = Label1.Text;
string searchText = null;
try
{
searchText = PreviousPage != null ? getSearchTextBoxText(PreviousPage) : getSearchTextBoxText(this);
}
catch(Exception ex)
{
log.Error("Exception occurred attempting to retrieve the search string", ex);
}
...