다음을 통해 공유


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

Comments

  • Anonymous
    March 23, 2007
    Hi! I have an master page with two placeholders. In the first one I have placed a TextBox control and in the second one I have placed a RequiredFieldValidator. I set the ControlToValidate property to ctl00$ContentPlaceHolder1$txtMyTextBox. With this setup I get an Unable to find control exception. Is it possible to solve this in any way?

  • Anonymous
    March 24, 2007
    Thank you! http://ykrmggxt.com/urua/sxgm.html | http://bbdcoffl.com/lvmw/xacm.html

  • Anonymous
    April 11, 2007
    Ich erklare meinen Freunden uber diese Seite. Interessieren!

  • Anonymous
    April 13, 2007
    Thanks for clearing this up for me, I'd been racking my brains as to why it wasn't working. Now I know! Thanks

  • Anonymous
    April 17, 2007
    So simple once it's all laid out for you. You've saved me a lot of frustration - wish I knew about this blog a couple of hours ago. Thanks.

  • Anonymous
    August 07, 2007
    The comment has been removed

  • Anonymous
    August 07, 2007
    The comment has been removed

  • Anonymous
    September 25, 2008
    How do you transfer a value in a label of a master page to the next master page in the master page page_load?