1

So i have a content page aspx & aspx.cs, i add to the aspx this line <%@ MasterType virtualPath="~/MasterPage.master"%> so i could pass my data from the aspx.cs to my master page. and it works, but once i click to move to another page or refresh the page the data i passed is gone and and the value return to the original default value the field had before i passed anything. in the master page i wrote:

public string PassLogedUser  
    {  
        get { return this.LogedUser.Text; }  
        set { this.LogedUser.Text = value; }  
    }  

protected void Page_Load(object sender, EventArgs e)  
    {  
        LogedUser.Text = PassLogedUser;  
    }  

How can i keep the data i pass when i navigate to another page in browser or even when i refresh the page?

Thanks

wpclevel
  • 2,451
  • 3
  • 14
  • 33
nirh1989
  • 199
  • 2
  • 6
  • 15
  • You can use Session to save data that is available to the same user from page to page. You could also store it in a cookie or in a database. –  Jun 10 '16 at 17:13
  • Thanks, can you please tell me how to do it? how to use session or direct me to a link with explanation? – nirh1989 Jun 10 '16 at 17:14
  • This is really basic stuff, and there is a lot of information available online regarding persisting data between pages in ASP.NET Web Forms. Google would be a good start. – DVK Jun 10 '16 at 17:15
  • 1
    https://msdn.microsoft.com/en-us/library/ms178581.aspx - The thing to realize with this is that data is stored on the session as an object and must be cast to the type you need when you retrieve it from the Session. –  Jun 10 '16 at 17:17
  • Thank you all guys, i will close this question now :) – nirh1989 Jun 10 '16 at 17:24

1 Answers1

1

All pages are stateless. Properties only live while the page is rendered and once the response is returned to the user the page state is gone.

To solve this, in the propertys setter you need to persist the value in a user-specific location. The global Session may be a good place - or a user Cookie. Then in the propertys getter you need to read from the storage you used.

public string PassLogedUser 
{ 
    get 
    { 
        return Session("PassLogedUser") ?? "anonymous";
    } 
    set 
    {
        Session("PassLogedUser", value);
    } 
} 

Then in the master page prerender event you need to set the LogedUser.Text = PassLogedUser;

Mattias Åslund
  • 3,877
  • 2
  • 18
  • 17