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

  • Thread starter Thread starter mortb
  • Start date Start date
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
 
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
 
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
 
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
 
Back
Top