Cross post with Master pages

I got a feedback for the Master Pages post asking about mixing master pages with cross post. Here's the scenario.

We have a content page that has one textbox and one button, the page code would be like this

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="master_form.aspx.vb" Inherits="master_form" title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<asp:TextBox ID="id" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" PostBackUrl="~/result.aspx" Text="Button" />

</asp:Content>

As you can see, the button posts to another page called result.aspx.

When you get the data posted using Request.Form("id") , this would result in empty string because there's no entry with this name in the form collection in the form collection. Because the content page is merged with the master page so the id of the textbox would be ctl00$ContentPlaceHolder1$id (ctl00 is the form element, and contentPlaceHoder1 is the id of the contentPlaceHolder) so if you would like to get it from the form collection, you would write it in the form of Request.Form("ctl00$ContentPlaceHolder1$id").

This technique is not appropriate as you can see, so the most appropriate technique is to use the PreviousPage property. PreviousPage is a property for the Page class that has a reference to the previous page if you are doing Server.Transfer or Cross Posting.

You can use PreviousPage and FindControl method to get any control you have on the previous page like this code snippet.

Dim ctlTextBox As TextBox = Nothing

If Not Page.PreviousPage Is Nothing Then

ctlTextBox = CType(PreviousPage.Form.FindControl("ContentPlaceHolder1").FindControl("id"), TextBox)

End If

Hope this helps J