ASP.Net c# - Get Textbox Value from Masterpage

Hi Experts,

I have a search textbox situated on a masterpage like so:

<asp:TextBox ID="frmSearch" runat="server" CssClass="searchbox"></asp:TextBox>
<asp:LinkButton ID="searchGo" PostBackUrl="search.aspx"  runat="server">GO</asp:LinkButton>

Open in new window


The code behind for the search page has the following to pick up the textbox value (snippet):

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
        {
            Page previousPage = PreviousPage;
            TextBox tbSearch = (TextBox)PreviousPage.Master.FindControl("frmSearch");
            searchValue.Text = tbSearch.Text;

            //more code here...
        }

Open in new window


All works great. BUT not if you enter a value whilst actually on search.aspx, which obviously isn't a previous page. How can I get round this dead end I've put myself in?
ComfortablyNumbAsked:
Who is Participating?
 
Kiran SonawaneProject LeadCommented:
Try like this

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack) {
          Page previousPage = PreviousPage;
            TextBox tbSearch = (TextBox)PreviousPage.Master.FindControl("frmSearch");
            searchValue.Text = tbSearch.Text;
//when the current page is PageC.aspx
} else {
      TextBox tbSearch = (TextBox)this.Master.FindControl("frmSearch");
      searchValue.Text = tbSearch.Text;
}
0
 
ComfortablyNumbAuthor Commented:
Thanks sonawanekiran. Worked straight out of the box!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.