Using PreviousPage with a Master Page
Using the PreviousPage scenario, with Master Pages presents its own particular problems, but they’re not hard to overcome. As you may have noticed, using “PreviousPage” scenario isn’t quite as simple as it looks in all the tutorials. In fact, using most of the tutorials/code samples on the web, doesn’t work at all. That’s specifically because they aren’t taking into consideration, that, with a Master Page, you have a ContentPlaceholder, and you must first look there, to find the data from the previous page.
For those who don’t know, the base samples show a scenario in which the ‘Submit’ button on the first page has it’s PostBackURL property assigned to the second page in the postback scenario. The code, then would look something like:
Dim Name As TextBox Name = Page.PreviousPage.FindControl("txtName") Label1.Text = Name.Text
As we stated earlier, if you’re using a Master Page in your website (with a Master Page designated, specifically, in the submitting page), this doesn’t work. In able to do this, you must look in the Content Placeholder of the submitting page, first. Therefore, the code would look more like this:
Dim ph As ContentPlaceHolder = CType(PreviousPage.Master.FindControl("YourContentPlaceholderID"), ContentPlaceHolder Dim tb As TextBox = CType(ph.FindControl(YourControlID), TextBox) Label1.Text = tb.Text
In this sample, we first look inside the submitting page, then we find the Contentplaceholder. Then, once we find the contentPlaceholder, we look inside it, to find the control, from which we need to retrieve the data. Here’s a small function which does all this for us, to cut down on repetitive code:
Function GetData(ByVal CtrlID As String) As String Dim ph As ContentPlaceHolder = CType(PreviousPage.Master.FindControl("YourContentPlaceholderID"), ContentPlaceHolder) If Not ph Is Nothing Then Dim tb As TextBox = CType(ph.FindControl(CtrlID), TextBox) If Not tb Is Nothing Then Return tb.Text End If End If Return String.Empty End Function
Then, to use it, you merely do something like this:
lblName.Text = GetData("txtName")
That’s all there is to it. Have fun!




15. Jan, 2008 








No comments yet... Be the first to leave a reply!