I am trying to take the value a user inputs from a form and put it into a session variable. The basic code (on Page1) is thus:
Session("first_name") = Request.Form("first_name")
If I reference the session variable on that same page (such as <%= Session("first_name") %>) it works fine. However, when I go to a new page (Page2) it does not work anymore, the session variable just shows up blank. If I go back to the first page and set the session variable equal to a simple string it works and will show up fine on both pages. Like thus:
Session("first_name") = "first name"
I realized what was happening is that Session("first_name") is not being set to the contents of Request.Form("first_name") but is instead pointing to the actual Request.Form object. If I include a <input type="hidden" name="first_name" value="some value here"> on Page2 then when I try to display Session("first_name") it displays "some value here" because it is now pointing to the Request.Form("first_name") on Page2 instead of on Page1.
Are session variables just pointers to the Request.Form variable? Is there a way to set it to the actual content of the input field so I can pass it to the next page?
![]() |
0 |
![]() |
If you're using TextBox, than instead of Request.Form use this:
Session("first_name") = TextBox1.Text
![]() |
0 |
![]() |
I don't think I'm using textBox. I'm not familiar with it, but I would assume that I would have had to specifically declare it.
Edit: If you're referring to <asp:Textbox> I am not using it. The tags are simple HTML <input>
![]() |
0 |
![]() |
Can you post your whole code (page and code-behind)?
![]() |
0 |
![]() |
Ok, there are three pages. I'm sure this isn't done according to the standard development model, as I'm new to ASP.Net and just trying to get this working.
Page 1 (input.aspx):
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
3 <script runat="server">
4
5 Sub SubmitBtn_Click(ByVal Sender As Object, ByVal E As EventArgs)
6 Server.Transfer("confirm.aspx", True)
7 End Sub
8
9 </script>
10
11 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
12
13 <head runat="server">
14
15 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
16 <title></title>
17
18 </head>
19 <body>
20
21 <form id="form1" runat="server">
22
23
24 <table>
25 <tr>
26 <td>First Name</td>
27 <td>
28 <input style="WIDTH: 250px; HEIGHT: 22px" size="44" name="first_name">
29 </td>
30 </tr>
31 <tr>
32 <td>Last Name</td>
33 <td>
34 <input style="WIDTH: 250px; HEIGHT: 22px" size="44" name="last_name">
35 </td>
36 </tr>
37 </table>
38
39 <asp:button text="Submit" OnClick="SubmitBtn_Click" runat="server" id="Button1" />
40
41 </form>
42 </body>
43 </html>Page 2 (confirm.aspx):1
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
3 <html>
4 <script runat="server">
5
6 Sub SubmitBtn_Click(ByVal Sender As Object, ByVal E As EventArgs)
7 Server.Transfer("submit.aspx", True)
8 End Sub
9
10 ' When the page loads
11
12 Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
13
14 Session("first_name") = Request.Form("first_name")
15 Session("last_name") = Request.Form("last_name")
16
17 Message.InnerHtml = "First Name: " & Session("first_name") & "<br/>Last Name:</td><td>" & Session("last_name")
18
19 End Sub
20
21 </script>
22
23 <head id="Head1" runat="server">
24
25
26 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
27 <title></title>
28 </head>
29
30 <body>
31
32 <form id="form1" runat="server">
33
34 Please Confirm these Details:
35
36 <div id="Message" runat="server" />
37
38 <asp:button text="Submit" OnClick="SubmitBtn_Click" runat="server" id="Button1" />
39
40 </form>
41 </body>
42 </html>Page 3 (submit.aspx):1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <html>
3
4 <head id="Head1" runat="server">
5
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <title></title>
8 </head>
9 <body>
10
11 You have submitted these details:
12
13 <%= Session("first_name") & Session("last_name") %>
14
15 </body>
16 </html>Everything works fine up to Page 3 (submit.aspx) where the Session variables at line 13 don't show up. If I go back to Page 2 (confirm.aspx) and change lines 14 and 15 thus, then the session variables show up fine in Page 3 (submit.aspx) as whatever strings I defined them as:14 Session("first_name") = "first_name"
15 Session("last_name") = "last_name"Or, if I include this on Page 2 (confirm.aspx) inside the <form> tag:1 <input type="hidden" name="first_name" value="My First Name Value"/>
2 <input type="hidden" name="last_name" value="My Last Name Value"/>Then on Page 3 (submit.aspx) the page returns the values I put in those <input> tags as the values for the Session variables. I presume that on Page 2 the session variables have somehow been set to point to the Request.Form objects rather than to the contents of the Request.Form objects coming from page 1.
![]() |
0 |
![]() |
You should use TextBox control instead input fields, so replace this:
with this:<input style="WIDTH: 250px; HEIGHT: 22px" size="44" name="first_name">and change your code in input.aspx into this:<asp:TextBox ID="first_name" runat="server" />Sub SubmitBtn_Click(ByVal Sender As Object, ByVal E As EventArgs)
Session("first_name") = first_name.Text
Server.Transfer("confirm.aspx", True)
End Sub
![]() |
0 |
![]() |
Incredible. Thanks for the help! I looked all over and wasn't finding anything. Maybe someday I'll be as comfortable with this as I am with PHP.
![]() |
0 |
![]() |