.NET Tips and Tricks

Blog archive

ASP.NET: Integrating Search and Help with Cross-Page Posting

By default, ASP.NET pages post their data back to themselves. However, there are cases that, when the user clicks on a button, you want the data in the browser sent to some other page. For instance, if you have an option that lets users search the site, you've probably implemented this with a Search button with a text box on every page on your site. When the user enters some text and clicks on the button, you want to send the user to your site's Search page, not back to the current page—and you also want to send to that page whatever search term the user entered in the text box beside the button. The same is true of a Help button with a text box: When the user clicks the button, you want to send the user to a page that displays help about the topic the user entered into the text box.

In ASP.NET, sending the data on a page to another page is called cross-page posting. It's controlled through a property on the ASP.NET Button. To turn on cross-page posting, set the button's PostBackUrl property to the URL of the page you want the data sent to (e.g., your search or help page).

However, when you arrive at the other page, you'll need to retrieve the value that the user entered in the search or help text box. Since you're now on a different page, the user's data won't be nicely slotted into a control on the form—you'll have to hunt for the text box. And, since the user can come to a page through many ways (the user may have bookmarked your search page, for instance, and used the bookmark to get to the page) you don't want to hunt for the text box unless you know it's there.

The Page Load event is a good place to put that code. You should begin by determining if the user has arrived at your page through cross-page posting. The first step is to check to see if the page's PreviousPage property is set: If the user came to your page through cross-page posting,  PreviousPage will be set to something.

However, PreviousPage will be set in at least two scenarios: if the user arrived at the page using cross-page posting, or if the user arrived through using Server.Transfer. So, once you know that the PreviousPage property has something in it, you should check its IsCrossPagePostBack property to see if user got there through a click on a postback button. The code looks like this in Visual Basic:

If Me.PreviousPage IsNot Nothing AndAlso _
   Me.PreviousPage.IsCrossPagePostBack = True Then

The next step is to find your textbox. For that you can use the PreviousPage's FindControl method, passing the Id of the TextBox you're looking for. You'll need to cast the result to the correct object type (and you should also deal with not finding the text box in case some other developer builds a page that posts to your search or help page):

Dim txt As TextBox
  txt = CType(Me.PreviousPage.FindControl _
                         ("SearchTextBox"), TextBox)
  If txt IsNot Nothing Then  
      RetrievedString = txt.Text
  End If

Finding the text box is a little more difficult if your control is inside a naming container like a Content control or a FormView. It wouldn't be surprising if your cross-page posting button is part of your site's Master Page (a Help or Search button would, typically, be something that you'd want on every page on the site). Fortunately, you can search a PreviousPage's Master page through its Master property, like this:

Dim txt As TextBox
  txt = Me.PreviousPage.Master.FindControl( _
     "HelpTopicTextBox"), TextBox)

Posted by Peter Vogel on 01/13/2012 at 1:16 PM

comments powered by Disqus


  • Creating a Progressive Web App with Blazor WebAssembly

    Not surprisingly, it's dead easy to create an app in Blazor that runs outside of the browser window and (potentially) in an offline mode. Before you get carried away, though, there are some key design decisions to make.

  • GitLab Takes Over VS Code Extension, Plans Improvements

    DevOps specialist GitLab has officially taken over the control of a GitLab extension for Microsoft's open source, cross-platform Visual Studio Code editor.

  • VS Code Python Tool Now Does Native Notebooks

    The Python Extension for VS Code Insiders team is previewing the newest implementation of notebooks, used frequently in data science with offerings such as Jupyter Notebooks.

  • As .NET 5 Nears, Content/Documentation Reorganization Starts

    A GitHub project is seeking to reorganize documentation and developer content in advance of the November debut of .NET 5, a unification of all things .NET that combines. .NET Core and other components.

  • Windows Devs Get Cross-Platform Page, Issues Repo

    Developers doing their coding on the Windows OS have received two new resource gifts from Microsoft: a new landing page for those using cross-platform technologies and a new GitHub repo with which to report issues to Windows engineering teams.Developers doing their coding on the Windows OS have received two new resource gifts from Microsoft: a new landing page for those using cross-platform technologies and a new GitHub repo with which to report issues to Windows engineering teams.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events