Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to pass the value of label on my MSG.aspx.cs to Default.aspx.cs.

I'm using a session here, does anyone know any other options on how to do this??

(If my question is not clear, The program is loading the Default.aspx.cs first then on Page_Load it should call the MSG.aspx.cs then get the value of label on MSG.aspx.cs)

What I have tried:

Default.aspx.cs
C#
protected void Page_Load(object sender, EventArgs e)
{
   lblName.Text = Session["name"].ToString();
}

MSG.aspx.cs
C#
Match sndrNameMatch = Regex.Match(body, @"From: ([A-Za-z0-9\-]+)", RegexOptions.IgnoreCase);
            
lblName.Text = sndrNameMatch.Value;
Session["name"] = lblName.Text;
Posted
Updated 19-Feb-16 2:41am
v2

1 solution

You can use Session, QueryString, or Application Cache from server side. From the client side you can use cookies or localStorage.

However, there is nothing wrong with using the session so I'd suggest just leaving it alone.
 
Share this answer
 
Comments
Member 11525563 19-Feb-16 8:38am    
On my code i'm having an error like this:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 43: prevEmail.Visible = false;
Line 44: }
Line 45: lblName.Text = Session["name"].ToString();
Line 46: }
Line 47:
Richard Deeming 19-Feb-16 8:50am    
Probably because you haven't set Session["name"] to anything at that point.
ZurdoDev 19-Feb-16 8:58am    
As Richard said, you haven't put anything into Session["name"]. Either initialize it somewhere or check for null first.

if (Session["name"] != null)
{
...
}
Member 11525563 19-Feb-16 9:03am    
That's the main problem, because on Page_load of Default.aspx.cs i want to get the value/Session of Session["name"] that is on MSG.aspx.cs
ZurdoDev 19-Feb-16 9:05am    
Ok, so check if it is null or not. Very simple.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900