Retrieve search string from text box on master page for internal search
<p>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.</p>

<p>This all works fine except when a search is entered from the log-in page due to an exception: <em>The current user is not allowed to access the previous page</em>.</p>

<p>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.</p>

<p>Below is the pertinet code:</p>

<p>From the master page:</p>

<pre><code>&lt;asp:TextBox ID="SearchTextBox" runat="server" /&gt;
&lt;asp:ImageButton ID="SearchButton" runat="server" Text="Search" PostBackUrl="~/Search.aspx" ImageUrl="~/Graphics/btn_search.png" ImageAlign="AbsMiddle" /&gt;
</code></pre>

<p>From the search results page:</p>

<pre><code>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);
    }
...
</code></pre>
