[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


ASP.Net c# - Get Textbox Value from Masterpage

Posted on 2011-10-17
Medium Priority
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

sonawanekiran earned 2000 total points
ID: 36978411
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

ID: 36978440
Thanks sonawanekiran. Worked straight out of the box!!!

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

873 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