Posting to another Page in ASP.NET

Posting values to another page is an extremely basic task when it comes to web programming. However, the ASP.NET model did not provide any simple way to achieve this. The ASP.NET model as compared to ASP, posts values to the same page.

ASP.NET 2.0 does provide a solution to some extent by providing Cross-Page Posting, see the Dino Esposito’s article if you want to learn more about Cross-Page Posting.

Recently I had to post certain values to a page of an external site. Cross Page Posting cannot be used in this case becuase the extrenal site is already in production and may not be develeped in ASP.NET at all.

There is another solution which can be used for posting values and that is to create a temporary HTML page with a standard HTML form with Action set to the page to which the values have to be posted. To achieve this there is wonderful RemotePost class (by Jigar Desai) which can be used to post values.

Dim rPost As RemotePost = New RemotePost()
rPost.Add(“DataVal”, “Data to be posted”)
rPost.Url = “” ‘ URL to which data is to be posted.
rPost.Post()

Public Class RemotePost
Private Inputs As New System.Collections.Specialized.NameValueCollection
Public Url As String = “”
Public Method As String = “post”
Public FormName As String = “form1”
Public Sub Add(ByVal name As String, ByVal value As String)
Inputs.Add(name, value)
End Sub
Public Sub Post()
System.Web.HttpContext.Current.Response.Clear()
System.Web.HttpContext.Current.Response.Write(“<html>”)
System.Web.HttpContext.Current.Response.Write(“<body onload=””document.form1.submit()””>”)
System.Web.HttpContext.Current.Response.Write(“”)
System.Web.HttpContext.Current.Response.Write(String.Format(“”, FormName))
System.Web.HttpContext.Current.Response.Write(String.Format(“<form name=””{0}”” method=””{1}”” action=””{2}””>”, FormName, Method, Url))
For i As Integer = 0 To Inputs.Keys.Count – 1
System.Web.HttpContext.Current.Response.Write(String.Format(“<input type=””hidden”” id=””{0}”” name=””{0}”” value=””{1}””/>”, Inputs.Keys(i), Inputs(Inputs.Keys(i))))
Next
System.Web.HttpContext.Current.Response.Write(“</form>”)
System.Web.HttpContext.Current.Response.Write(“</body></html>”)
System.Web.HttpContext.Current.Response.Write(“”)
‘System.Web.HttpContext.Current.Response.End()
End Sub
End Class

Advertisements