Passing a session variable from page to page

I'm wondering what my problem is when passing a variable from page to page using an asp.net session.

I split the code to a single text field to find out what was going on. I'm just trying to accept the value of a text field and display it on the confirmation page. When the button is pressed, it takes me to the second page, but there the label is empty. Yes, my backlink address points to the second page.

Here is the button:

protected void submit_Click(object sender, EventArgs e) { string name = txtFirstName.Text.Trim(); Session["name"] = name; } 

Here is the download page of the second page:

 protected void Page_Load(object sender, EventArgs e) { lblName.Text = (string)(Session["name"]); } 

If I had not looked at this for a long time and missed nothing. I already read "How to read values ​​from session state" from MSDN.

+6
source share
7 answers

You say that you installed PostBackUrl on your second page. If you are going to do this, you need to use Page.PreviousPage to access your text field. But this is the easiest way:

First, leave only PostBackUrl. Setting PostBackUrl on the second page means that you say SECOND PAGE to handle the button, not the first page. Therefore, your session variable will never be set and will be zero when you try to pull it out.

This should work for ya.

And yes, you can also do this with QueryString, but if this is what you do not want the user to see / edit, then it is better to use the Session variable.

  protected void submit_Click(object sender, EventArgs e) { string name = txtFirstName.Text.Trim(); Session["name"] = name; Response.Redirect("PageTwo.aspx"); } 

Then on the second page (you don't need ToString ()):

  protected void Page_Load(object sender, EventArgs e) { if (Session["name"] != null) { lblName.Text = Session["name"].ToString(); } } 

EDIT . Make sure the button is pressed. Someone may fix me wrong, since most of my work I do is in VB.NET, and not in C #. But if you do not specify an OnClick value, your function will not be called.

  <asp:Button ID="Button1" runat="server" Text="Click Me!" OnClick="submit_Click" /> 
+6
source

The code you posted looks great, so your problem is probably setup related.

Check out this ASP.NET Session Status Overview link and pay special attention to the topics on cookiesless SessionIDs and setting session state.

0
source

I do not think you have added a session. So I did.

First page

 protected void btnView_Click(object sender, EventArgs e) { foreach (ListItem li in lbxCheckDates.Items) { if (li.Selected == true) { lblMessage.Text = ""; string checkDate = lbxCheckDates.SelectedItem.Text; Session.Add("CheckDates", checkDate); Page.ClientScript.RegisterStartupScript( this.GetType(), "OpenWindow", "window.open('Paystub.aspx','_newtab');", true); } } } 

Second page

 protected void Page_Load(object sender, EventArgs e) { string checkDate = (string)(Session["CheckDates"]); //I use checkDate in sql to populate a report viewer } 

So with yours, I think you need ...

 protected void submit_Click(object sender, EventArgs e) { string name = txtFirstName.Text.Trim(); Session.Add("Name", name); } 

I think that what you have on the second page should work, but if it is not, add ToString () to it as ..

 lblName.Text = (string)(Session["name"]).ToString(); 

Let me know if this helps!

0
source

Do you redirect after setting the session variable on the first page if that doesn't help you (if you don't know the trick). Checkout this article for it to work. Basically, the way to do this work is overload redirection.

 Response.Redirect("~/newpage.aspx", false); 

An invalid setting prevents .net from completing processing on an existing page (which actually records session state)

0
source

For the second page

 protected void Page_Load(object sender, EventArgs e) { if (Session["value"] != null) { Label1.Text = Session["value"].ToString(); } else { Label1.Text = "Sorry,Please enter the value "; } } 
0
source

You can use Server.Transfer() instead of Response.Redirect()

0
source

For the first page, use this:

 protected void Button1_Click(object sender, EventArgs e) { string value = TextBox1.Text; Session["value"] = value; Response.Redirect("~/Sessionpage.aspx"); } 
-2
source

Source: https://habr.com/ru/post/956980/


All Articles