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 _
If txt IsNot Nothing Then
RetrievedString = txt.Text
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( _
Posted by Peter Vogel on 01/13/2012 at 11:56 AM