2

Can anyone give me a working example of PostbackUrl where both the target page and previous page have masterpage.

For example, let assume I have two pages default1.axpx and default2.aspx. Both of them have a masterpage MyMasterpage.masterpage

I want to postback from default1.aspx to default2.aspx and then extract data from default1 page controls in default2 page.

How can I do that?

th1rdey3
  • 4,176
  • 7
  • 30
  • 66
  • I don't understand what the master pages have to do with anything. Master pages are actually child controls of the page, not the other way around. As such, a master page is no different from any other control that may be on the page. – Erik Funkenbusch Sep 30 '12 at 18:48
  • @MystereMan the **PreviousPage** doesn't work as it should if the pages have masterpage. the example given by Rajpurohit won't work if the pages have masterpage. – th1rdey3 Oct 01 '12 at 04:15
  • Yes, the PreviousPage does in fact work as it should if the pages have a masterpage. The problem is that you don't understand how it's supposed to work, only what you assume will work. – Erik Funkenbusch Oct 01 '12 at 05:17
  • 2
    @MystereMan yes.. maybe i don't understand. hence i asked the question. to show me how its done with masterpage. if you would be kind enough to give me an woking example, i'll be grateful. – th1rdey3 Oct 01 '12 at 05:29

2 Answers2

7

You should have titled this question "How do I find a control in a ContentPlaceholder?", because your problem is not that PreviousPage doesn't work, it's that you don't understand how ContentPlaceholders work.

This problem has nothing to do with a master page per se, and is entirely related to using a ContentPlaceholder, which is a Naming Container in asp.net parlance. FindControls does not search inside Naming Containers, which is exactly how they are designed.

PreviousPage works fine with master pages, thus my confusion about what they have to do with your problem. You can access any property on the previous page that you want, and it will in fact work. For example:

 HtmlForm form = PreviousPage.Form; // this works fine
 Control ctrl = PreviousPage.Master.FindControl("TextBox1"); // this works as well

The problem you are likely encountering is that you're trying to use FindControl() to find a specific control in the content page, and it's not working, precisely because you're calling FindControl on the PreviousPage, and not on the naming container that the control you're looking for exists in.

To find the control you want, you just have to do a FindControl on the naming container. The following code works fine, assuming the placeholder is named ContentPlaceHolder1.

var ph = PreviousPage.Controls[0].FindControl("ContentPlaceHolder1");
var ctl = ph.FindControl("TextBox1");

You can verify that this problem has nothing to do with PreviousPage by using the following code, which uses only a single page and looks for the control on itself. Place a textbox on your Default.aspx page called TextBox1. Then, in the Page_Load function of the code behind of Default.aspx.cs put this code, then run it in the debugger and step through it.

protected void Page_Load(object sender, EventArgs e)
{
    // Following code should find the control, right?  Wrong. It's null
    var ctrl = Page.FindControl("TextBox1"); 

    // assuming your content placeholder in the masterpage is called MainContent, this works.
    var ctrl = Page.Controls[0].FindControl("MainContent").FindControl("TextBox1");
}

So please, don't go saying things like "the PreviousPage doesn't work as it should if the pages have masterpage", because it's working EXACTLY as it should. The problem is that you don't know how it should be working. Learn how the page object model works.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • If its possible then I would give you 10 votes for this answer. I had been struggling with this issue for last 30 min. Although I figured out that its the problem with ControlID but had no clue how to overcome. Thanks!! – sunny Dec 31 '13 at 21:50
  • For those coming to this now, if you are finding PreviousPage *always* to be null it might be helpful to read https://www.mikesdotnetting.com/article/293/request-form-is-empty-when-posting-to-aspx-page - basically URL's are being redirected after the POST with a GET and clearing the previous page data by default in Web Forms apps created in VS2013+ – Darren Ringer Apr 23 '20 at 17:13
0

you try this

this is first page

<%@ Page Language="C#" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e) {  
        Label1.Text = "Your city name: " +  
            TextBox1.Text.ToString();  
    }  
</script>  

<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>PostBackUrl Example: how to submit a page to another page in asp.net</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="DarkSlateBlue"></asp:Label>  
        <br />  
        <asp:Label ID="Label2" runat="server" Text="Your City" AssociatedControlID="TextBox1"></asp:Label>  
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Text="*"></asp:RequiredFieldValidator>  
        <br />  
        <asp:Button ID="Button1" runat="server" Text="Submit in this page" OnClick="Button1_Click" />  
        <asp:Button ID="Button2" runat="server" Text="Submit in city page" PostBackUrl="~/CityPage.aspx" />  
    </div>  
    </form>  
</body>  
</html> 

In Second Page

<%@ Page Language="C#" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

<script runat="server">  
    protected void Page_Load(object sender, System.EventArgs e) {  
        TextBox PP_TextBox1;  
        PP_TextBox1 = (TextBox)PreviousPage.FindControl("TextBox1");  
        Label1.Text = "Your City: " +  
            PP_TextBox1.Text;  
    }  
</script>  

<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>City Page</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h1>City Page</h1>  
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="Crimson"></asp:Label>  
    </div>  
    </form>  
</body>  
</html> 

i think this will help you....

Rajpurohit
  • 1,951
  • 2
  • 16
  • 19