Click here to Skip to main content
13,144,309 members (32,942 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 16 Jan 2007

Post form data to another URL in ASP.NET 1.1

, 16 Jan 2007
Rate this:
Please Sign up or sign in to vote.
I am a web developer. I worked for oil company in China before I came to Canada.

Introduction<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>

I need post form data to another URL in ASP.NET 1.1. After I searched it on internet, I found following methods. <o:p>

1. Use "Response.Clear()", "Response.Write()" and "Response.End()". It could post data to same page, but it could not post data to URL in a new window. Also I will will break "Back" button in browser. <o:p>

2. Use HttpWebRequestion and HttpWebResponse to send request to server and get reponse. It returnes all page information in HTML. So we have to go through HTML to find useful data. <o:p>

3. Post form data using Javascript. But I could not use it in user control, since I could not create multiple forms dynamically.<o:p>

Demo Codes<o:p>

I would like give some demo codes for 3 methos as following,<o:p>

1. <o:p>

public class RemotePost
private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection();

public string Url = "";
public string Method = "post";
public string FormName = "form1";

public void Add(string name,string value)

public void Post()


System.Web.HttpContext.Current.Response.Write(string.Format("</head><body onload=\"document.{0}.submit()\">",FormName));
System.Web.HttpContext.Current.Response.Write(string.Format("<form name=\"{0}\" method=\"{1}\" action=\"{2}\" >",FormName,Method,Url));
for(int i=0;i< Inputs.Keys.Count;i++)
System.Web.HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">",Inputs.Keys[i],Inputs[Inputs.Keys[i]]));



RemotePost m = new RemotePost();
m.Url = "test.aspx";

m.Add( "para1" ,"123456" );
m.Add( "para2" ,"0001" );

Please pay attention on "onload" event, form will be posted automatically.<o:p>


2. <o:p>


<%@ Import Namespace="System.Net" %>

<%@ Import Namespace="System.IO" %>

<script language="C#" runat="server">

   void Page_Load(Object Src, EventArgs E) {

      myPage.Text = readHtmlPage("");


   private String readHtmlPage(string url)


      String result = "";

      String strPost = "x=1&y=2&z=YouPostedOk";

      StreamWriter myWriter = null;


      HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);

      objRequest.Method = "POST";

      objRequest.ContentLength = strPost.Length;

      objRequest.ContentType = "application/x-www-form-urlencoded";




         myWriter = new StreamWriter(objRequest.GetRequestStream());



      catch (Exception e)


         return e.Message;


      finally {




      HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();

      using (StreamReader sr =

         new StreamReader(objResponse.GetResponseStream()) )


         result = sr.ReadToEnd();

         // Close and clean up the StreamReader



      return result;





<b>This content is being populated from a separate HTTP request to

<a href=""></a>:</b><hr/>

<asp:literal id="myPage" runat="server"/>





<div id="Content" name="Content"><o:p>

    <form method="post" id="Form1" runat="server"><o:p>




The JavaScript now needs to differentiate between Netscape 4 and the other DOM aware browsers. Check for document.layers to identify Netscape 4, and simply use the syntax appropriate for that browser:<o:p>

<script language="javascript"><o:p>


function noPostBack(sNewFormAction)<o:p>


    if(document.layers) //The browser is Netscape 4<o:p>


        document.layers['Content'].document.forms[0] = <o:p>


        document.layers['Content'].document.forms[0].action = <o:p>



    else //It is some other browser that understands the DOM<o:p>


        document.forms[0].action = sNewFormAction;<o:p>

        document.forms[0] = 'NOVIEWSTATE';<o:p>









My coworker gave me some good ideas, I choose 3rd method and improve it as my solution. Everytime, form and hidden fields are created dynamically, so it could handle form and fields as many as I wish. Also it could be used in user control, since all function is done on client side.<o:p>


//Creat form dynamically and post data to specified URL
//formName: the name of form
//formAction: the URL
//hidden input fields, like fieldname1=fieldvalue1&fieldname2=fieldvalue2
function postForm(formName,formAction,arrayStr,newWindow)
var frm=cf(formName,formAction,newWindow);
var inputArray=arrayStr.split('&');
for(var j=0;j<inputArray.length;j++)
var temp=inputArray[j].split('=');
if(isArray(temp) && temp.length==2)
//Check whether variable is array
function isArray(obj) {
if (obj.constructor.toString().indexOf('Array') == -1)
return false;
return true;
function cf(name,action,newWindow) {
var f = document.createElement('FORM');
return f;
function cfi(parentForm,name,value) {
var i = document.createElement('INPUT');


in ASP.NET,<o:p>

HyperLink hl;<o:p>

string arrStr="fieldname1=fieldvalue1&fieldname2=fieldvalue2";<o:p>


hl.Attributes.Add("onclick","postForm('PostedData','getpost.aspx','"+arrStr+"',true);return false;");<o:p>


If anyone knows how to implement same function on server side, please let me know. Thanks.<o:p>



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralExactly what I needed Pin
sparks_2193-May-07 6:44
membersparks_2193-May-07 6:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 17 Jan 2007
Article Copyright 2007 by baoshenyi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid