How do we get the value from the hidden field ?

B

bienwell

Hi,

I've got a problem of getting the value from the hidden field in the
previous page when I'm in the next page:

I have 2 web pages. In the first page I have a hidden field, a text field
and a button. When I click on the button, the text field would assign the
value to the hidden field, and the page redirect to the next page. In the
next page, I'd like to get the value from the hidden field from the first
page. My problem is I got the blank value from the hidden field. The code
looks like this:


1- First page:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub btnProceed_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

Me.myHiddenField.Value = TextBox1.Text

Response.Redirect("RequestForm.aspx")

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server" >

<div>

<asp:hiddenfield id="myHiddenField" runat="server"/>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

<br />

<asp:Button ID="btnProceed" runat="server" OnClick="btnProceed_Click"
Text="Proceed" /></div>

</form>

</body>

</html>

=========================================================

Second page:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Sub Page_Load()

Response.Write("<BR>Value ==> " & Request.Form("myHiddenField"))

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>


</div>

</form>

</body>

</html>

==========================================

Did I do something wrong ? Please advise!

Thanks in advance.
 
M

Mark Rae [MVP]

When I click on the button, the text field would assign the value to the
hidden field

This needs to be done client-side - it can't be done server-side...

Me.myHiddenField.Value = TextBox1.Text

Remove that line.

<asp:Button ID="btnProceed" runat="server" OnClick="btnProceed_Click"
Text="Proceed" />

<asp:Button ID="btnProceed" runat="server" OnClick="btnProceed_Click"
OnClientClick="document.getElementById('<%=myHiddenField.ClientID%>').value=document.getElementById('<%=TextBox1.ClientID%>').value;"
Text="Proceed" />
 
B

bienwell

Hi,

The problem was not client -side or server side. I followed the instruction
in this forum. This code worked fine in server-side. You can have this line
Response.Write("<BR>Hidden Value==> " & myHiddenField.Value)

instead of

Response.Redirect("RequestForm.aspx")

in the first page to see the assignment worked fine.


My problem is how to get the value from the hidden field in the second page
by suing Request.Form("myHiddenField.") ?
 
P

Patrice

Data are posted to a page. So you post data to this page and then you tell
the browser to direct to another page loosing previously posted data.

You have several approach :
- store this at some other location (for example in a DB, session variable,
querystring, cookies etc.. where you'll be able to read on the next page)
- use transfer rather than redirect. You have an option that preserve posted
values but the URL change won't be seen by the browser
- post directly to the next page
- post to the same page and display a different "view"
- and probably others...

Also the hidden field is not terribly usefull as basically you don't use it
(it creates an hidden field not used in the first page, then you fill this
hidden field but as you won't create HTML as you redirectn, it is basically
not used).

The key point is that data coming with the HTTP request are to be used
during the same HTTP requests. If you want to use them during later HTTP
requests you have to save them somewhere.
--
Patrice


bienwell said:
Hi,

The problem was not client -side or server side. I followed the
instruction in this forum. This code worked fine in server-side. You can
have this line
Response.Write("<BR>Hidden Value==> " & myHiddenField.Value)

instead of

Response.Redirect("RequestForm.aspx")

in the first page to see the assignment worked fine.


My problem is how to get the value from the hidden field in the second
page by suing Request.Form("myHiddenField.") ?
 
B

bienwell

Thank you very much for your information.

Patrice said:
Data are posted to a page. So you post data to this page and then you tell
the browser to direct to another page loosing previously posted data.

You have several approach :
- store this at some other location (for example in a DB, session
variable, querystring, cookies etc.. where you'll be able to read on the
next page)
- use transfer rather than redirect. You have an option that preserve
posted values but the URL change won't be seen by the browser
- post directly to the next page
- post to the same page and display a different "view"
- and probably others...

Also the hidden field is not terribly usefull as basically you don't use
it (it creates an hidden field not used in the first page, then you fill
this hidden field but as you won't create HTML as you redirectn, it is
basically not used).

The key point is that data coming with the HTTP request are to be used
during the same HTTP requests. If you want to use them during later HTTP
requests you have to save them somewhere.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top