0

I've looked at several similar questions to this one and right now I am trying to use the FindControl method without success.

My Web Forms application has several master pages. Common to all master pages is a search box and button in a nav user control at the top. I'm trying to grab the search term entered on the SearchResults page:

HtmlGenericControl topNavDiv = (HtmlGenericControl)Master.FindControl("topNavDiv");
Control topNav = (UserControl)Page.FindControl("topNav");
if (topNav != null)
{
    TextBox searchBox = topNav.FindControl("searchBox") as TextBox;
    if (searchBox != null)
    {
        Response.Write(searchBox.Text.Trim());
    }
    else
    {
        resultsPanel.Visible = false;
        messagePanel.Visible = true;
     }
}

In the first line, the topNavDiv variable is null. I was considering accessing the user control via a master page property, but with several different master pages I don't know how to determine the id of the master page as the search could be initiated from anywhere in the site...

UPDATE:

I was able to grab the topNav div, searchBox textbox as follows:

Control topNav = (UserControl)Master.FindControl("topNav");
if (topNav != null)
{
    TextBox searchBox = topNav.FindControl("searchBox") as TextBox;
    if (!String.IsNullOrEmpty(searchBox.Text))
    {
        Response.Write(searchBox.Text.Trim());
    }
    else
    {
        resultsPanel.Visible = false;
        messagePanel.Visible = true;
    }
}

The only problem is that the search box text entered is not being persisted.

Community
  • 1
  • 1
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91

2 Answers2

0

Since this is an Intranet site, I was able to utilize a query string parameter to obtain the search term as follows:

Nav with Search Box User Control:

<ul class="navbar-form navbar-left">
    <li>
        <asp:TextBox ID="searchBox" class="form-control col-lg-8" 
            placeholder="Search" runat="server"></asp:TextBox>&nbsp;                  
        <asp:Button ID="searchButton" CssClass="btn btn-warning" Text="Search"
            runat="server" OnClick="searchButton_Click" />
    </li> 
</ul>

User Control Code-Behind:

protected void searchButton_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Search/SearchResults.aspx?term="+searchBox.Text.Trim());
}

Search Results Page Code-Behind:

if (!Page.IsPostBack)
{
    if (!String.IsNullOrEmpty(Request.QueryString["term"]))
    {
        term = Request.QueryString["term"].ToString();
        Response.Write(term);
    }
    else
    {
        resultsPanel.Visible = false;
        messagePanel.Visible = true;
    }
}

What I would really like to know is if it is programmatically possible to determine in a page's code-behind, the identity of the master page of the requesting page?

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
0

If you need to keep the data out of the query string for some reason you could use a Property on your MasterPage.cs file.

public string SearchTerm
{
    get
    {
        return searchBox.Text;
    }
}

Then you can use that wherever you need to like this:

MasterPage master = (MasterPage)this.Master;
master.SearchTerm;
asven
  • 133
  • 1
  • 7
  • this.Master would be the master page of the Search Results page - user can enter search term from any one of five master pages, so not sure if this will work... – IrishChieftain Jun 27 '14 at 20:37