-2

I have two pages page1.aspx and page2.aspx, both have code behind with partial classes. How do i access public property message on page1.aspx from page2.aspx ?

public string message { get; set; }
eugeneK
  • 10,750
  • 19
  • 66
  • 101
  • Is the value of this property different for each page? – codingbadger Jun 14 '10 at 10:26
  • @Barry, what do you mean by that ? – eugeneK Jun 14 '10 at 10:28
  • I don't see why they should. One page will be loaded and the other won't be as normal behaviour. – Moox Jun 14 '10 at 10:29
  • @Moox, project is compiled and every page is a class by it's own. I don't see reason why they shouldn't be accessible. Then whats the reason behind class for each page ? – eugeneK Jun 14 '10 at 11:14
  • @eugeneK, it's unclear in your question that page2.aspx is the master page. you might want to edit your question. – Moox Jun 14 '10 at 11:17
  • @Moox, it doesn't really matters whether it a page or MasterPage if i can't access public properties without setting sort of VirtualPath in both cases. Thanks anyway. – eugeneK Jun 14 '10 at 11:30
  • @eugeneK I see that somewhere you say that the one othe page is masterPage ! If so the one is the MasterPage and the other is a page that use the master then the solution is diferent. ! Just search how to get Parametres from Master Page to the page that use it. ! – Aristos Jun 14 '10 at 11:45
  • @Aristos, point is i need solution without use of VirtualPath. I asked this question that may sound silly because i re-phrased another question i've asked earlier with answers that doesn't compile. If you want to help me then it's here http://stackoverflow.com/questions/3031992/how-can-access-public-properties-of-masterpage-from-external-class – eugeneK Jun 14 '10 at 12:18
  • @eugeneK I have post one other answer with a master-client page, maybe this is what you need for ? – Aristos Jun 14 '10 at 13:01

5 Answers5

2

Update

I just read that the one is MasterPage and the other is the client to masterpage ? then its diferent way.

Page to Page

If you have 2 simple diferent pages. I have done it this way. Its a post value, by using asp.net tricks :)

On Page2.aspx add this on top.

<%@ PreviousPageType VirtualPath="Page1.aspx" %>

and how I read from Page1.aspx on code behind

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (Page.PreviousPage != null)
        {
            if(Page.PreviousPage.IsCrossPagePostBack == true)
            {
                txtGetItFromPreviusPage.Text = PreviousPage.SomeString;
            }
        }
    }
}

On Page1.aspx the button that send me to Page2.aspx

<asp:Button ID="btnEna" runat="server"  Text="Send Some variables to other page" 
    PostBackUrl="Page2.aspx" 
    onclick="btnMoveSelection_Click" />                 

and the code that I use for Page1 calculations or other thinks

public string SomeString
{
    set 
    { 
        ViewState["txtSomeString"] = value;
    }
    get
    {
        if (ViewState["txtSomeString"] != null)
            return ViewState["txtSomeString"].ToString();
        else
            return string.Empty;
    }
}


protected void btnMoveSelection_Click(object sender, EventArgs e)
{
    // some final calculations

}   
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • is it possible without <%@ PreviousPageType VirtualPath="~/Page1.aspx" %> ? – eugeneK Jun 14 '10 at 11:12
  • @eugeneK It is not possible with out the PreviousPageType, and its not possible with out post from one page to the other the data. Actually what every one say here is TRUE, for access data from other page you need to post it ether in form, ether in URL. – Aristos Jun 14 '10 at 11:20
1

If the one is the Master page, and the other is the page that use the master.

The Master Page

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Literal runat="server" ID="txtOnMaster"></asp:Literal>
        <br />
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>

and the code behind

public partial class Dokimes_StackOverFlow_MasterPage : System.Web.UI.MasterPage
{
    public string TextToMaster
    {
        get { return txtOnMaster.Text; }
        set { txtOnMaster.Text = value; }        
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        // here I find the control in the client page
        Control FindMe = ContentPlaceHolder1.FindControl("txtOut");

        // and if exist I set the text to client from the master   
        if (FindMe != null)
        {
            ((Literal)FindMe).Text = "Get from Master Page";
        }
    }
}

and now the Page1.aspx that have the previus master page

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Literal runat="server" ID="txtOut"></asp:Literal>  
</asp:Content>

and the code

protected void Page_Load(object sender, EventArgs e)
{
    // here I set the text on master page from client
    ((Dokimes_StackOverFlow_MasterPage)Master).TextToMaster = "Set from Client";
}
Aristos
  • 66,005
  • 16
  • 114
  • 150
1

If you are NOT in a sessionless environment, then in the transmitter page, push your string (or your object - e.g., a Dictionary ) into session:

Session("MyVar") = "WhatEver"

In the receiver page, you can get it back with:

MyPreviousVar = Session("MyVar")
rajah9
  • 11,645
  • 5
  • 44
  • 57
eliberatus
  • 11
  • 1
0

You shouldn't really be doing this, pages should be standalone entities. If you need to pass this data from one form to another, consider using the querystring, or posting your form to the second page.


OK. Have you tried then Page.Master.Property?

Paddy
  • 33,309
  • 15
  • 79
  • 114
  • page2.aspx is MasterPage where i want to pass parameter and fail to do so. – eugeneK Jun 14 '10 at 10:28
  • "Page.Master.Property" won't work unless i set VirtualPath on page. I need that to be done pro-grammatically without use of server tags – eugeneK Jun 14 '10 at 12:20
0

If you want a message property on every page. You could implement your own BasePage and define the message property in your base page. Then derive subsequent pages from your custom base page. That way all of your pages will always have a message property.

However, this isn't going to keep the message property constant through out each page. If you are trying to pass values between pages then you should use either session state or a querystring

This MSDN page may be of use to you.

codingbadger
  • 42,678
  • 13
  • 95
  • 110
  • problem is that i cannot set <%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %> via code. So i cannot have external class accessing page properties but only pages with VirtualPath set on them – eugeneK Jun 14 '10 at 10:53