ASP.Net c# - Get Textbox Value from Masterpage

Posted on 2011-10-17
Last Modified: 2012-08-14
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?
Question by:ComfortablyNumb
    LVL 17

    Accepted Solution

    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;

    Author Closing Comment

    Thanks sonawanekiran. Worked straight out of the box!!!

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now