.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

Reader Comments:

Wed, Jan 18, 2012 Peter Vogel Canada

Richard: I just noticed that your first post on using JavaScript to integrate the pages has gone missing! I'll see if it's retrievable because it was a good tip. Your note on using the PreviousPageType is also good (and I should have mentioned it). It's not useful in the scenario I picked where multiple pages on the same site will point to the Help or Search page UNLESS, perhaps, the developer creates a base page for the site that all other pages inherit from. In that case (I assume) the PreviousPageType could specify the base page type that all pages share. Having said that, I'm not sure if the mechanics of that would help in the scenarios I used in the article. Worth investigating

Wed, Jan 18, 2012 Peter Vogel Canada

Richard: Good suggestion! I'm willing to fight with FindControl rather than write JavaScript, I guess. And, with crosspage posting I don't have to worry about what's included in the querystring--my target page has access to everything on the requesting page (which, I suppose, is a different kind of inefficiency).

Wed, Jan 18, 2012 Richard

Alternatively, if you know that the cross-page postback will always come from a particular page, or a page inheriting from a particular base class, you can use the @PreviousPageType directive to give you a strongly-typed PreviousPage property, and expose the relevant data as properties:

http://msdn.microsoft.com/en-us/library/ms228169.aspx

http://msdn.microsoft.com/en-us/library/ms178139.aspx#GettingPublicPropertyValuesFromSourcePage

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.