Is there any way to do a post to another page in the code behind?

M

mortb

Sometimes you want to transfer to another page and pass some values along
not using the query string nor the session.
Is there some way to do this?

cheers,
mortb
 
K

Ken Cox [Microsoft MVP]

You can do a post using the WebClient class. Here's some sample code. The
values to post go in the NameValueCollection

Sub DoPost()
Dim uriString As String = _
"http://localhost/p4320work/mySpecialPage.aspx"
Dim strGoName As String
strGoName = TextBox1.Text
' Create a new WebClient instance.
Dim myWebClient As New System.Net.WebClient
Dim myNameValueCollection As New _
System.Collections.Specialized.NameValueCollection
myNameValueCollection.Add("go1", strGoName)
myNameValueCollection.Add("Button1", "")
Dim responseArray As Byte() = myWebClient.UploadValues _
(uriString, "POST", myNameValueCollection)

Label1.Text = "Response received was :" & _
System.Text.Encoding.ASCII.GetString(responseArray)
End Sub

Ken
Microsoft MVP [ASP.NET]
Toronto
 
M

mortb

Is it not possible to go directly to the page like response.redirect but
using a post instead?
As if the form would post to another page?

/mortb

Ken Cox said:
You can do a post using the WebClient class. Here's some sample code. The
values to post go in the NameValueCollection

Sub DoPost()
Dim uriString As String = _
"http://localhost/p4320work/mySpecialPage.aspx"
Dim strGoName As String
strGoName = TextBox1.Text
' Create a new WebClient instance.
Dim myWebClient As New System.Net.WebClient
Dim myNameValueCollection As New _
System.Collections.Specialized.NameValueCollection
myNameValueCollection.Add("go1", strGoName)
myNameValueCollection.Add("Button1", "")
Dim responseArray As Byte() = myWebClient.UploadValues _
(uriString, "POST", myNameValueCollection)

Label1.Text = "Response received was :" & _
System.Text.Encoding.ASCII.GetString(responseArray)
End Sub

Ken
Microsoft MVP [ASP.NET]
Toronto

mortb said:
Sometimes you want to transfer to another page and pass some values along
not using the query string nor the session.
Is there some way to do this?

cheers,
mortb
 
P

Patrice

You can :
- alter the action attribute of the form tag using JavaScript
- on google you'll find form controls adding the explicit handling of the
form attribute
- the next release also adds support for this

You could use also Server.Transfer (if you don't need the browser to see the
final URL)

Patrice

--

mortb said:
Is it not possible to go directly to the page like response.redirect but
using a post instead?
As if the form would post to another page?

/mortb

Ken Cox said:
You can do a post using the WebClient class. Here's some sample code. The
values to post go in the NameValueCollection

Sub DoPost()
Dim uriString As String = _
"http://localhost/p4320work/mySpecialPage.aspx"
Dim strGoName As String
strGoName = TextBox1.Text
' Create a new WebClient instance.
Dim myWebClient As New System.Net.WebClient
Dim myNameValueCollection As New _
System.Collections.Specialized.NameValueCollection
myNameValueCollection.Add("go1", strGoName)
myNameValueCollection.Add("Button1", "")
Dim responseArray As Byte() = myWebClient.UploadValues _
(uriString, "POST", myNameValueCollection)

Label1.Text = "Response received was :" & _
System.Text.Encoding.ASCII.GetString(responseArray)
End Sub

Ken
Microsoft MVP [ASP.NET]
Toronto

mortb said:
Sometimes you want to transfer to another page and pass some values along
not using the query string nor the session.
Is there some way to do this?

cheers,
mortb
 

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

Top