Introduction<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)
{
Inputs.Add(name,value);
}
public void Post()
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Write("<html><head>");
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]]));
}
System.Web.HttpContext.Current.Response.Write("</form>");
System.Web.HttpContext.Current.Response.Write("</body></html>");
System.Web.HttpContext.Current.Response.End();
}
}
In ASP.NET:
RemotePost m = new RemotePost();
m.Url = "test.aspx";
m.Add( "para1" ,"123456" );
m.Add( "para2" ,"0001" );
m.Post();
Please pay attention on "onload" event, form will be posted automatically.<o:p>
<o:p>
2.http://authors.aspalliance.com/stevesmith/articles/netscrape2.asp <o:p>
/stevesmith/articles/examples/cs/postscrape.aspx<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("http://aspalliance.com/aldotnet/examples/posttest.asp");
}
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";
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
return e.Message;
}
finally {
myWriter.Close();
}
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
</script>
<html>
<body>
<b>This content is being populated from a separate HTTP request to
<a href="http://aspalliance.com/aldotnet/examples/posttest.asp">
http://aspalliance.com/aldotnet/examples/posttest.asp</a>:</b><hr/>
<asp:literal id="myPage" runat="server"/>
</body>
</html><o:p>
<o:p>
3. http://www.codeproject.com/aspnet/jsnopostback.asp<o:p>
<div id="Content" name="Content"><o:p>
<form method="post" id="Form1" runat="server"><o:p>
<o:p>
</form><o:p>
</div><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>
<!--<o:p>
function noPostBack(sNewFormAction)<o:p>
{<o:p>
if(document.layers) //The browser is Netscape 4<o:p>
{<o:p>
document.layers['Content'].document.forms[0].__VIEWSTATE.name = <o:p>
'NOVIEWSTATE';<o:p>
document.layers['Content'].document.forms[0].action = <o:p>
sNewFormAction;<o:p>
}<o:p>
else //It is some other browser that understands the DOM<o:p>
{<o:p>
document.forms[0].action = sNewFormAction;<o:p>
document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';<o:p>
}<o:p>
}<o:p>
--><o:p>
</script><o:p>
<o:p>
<o:p>
Improvement.<o:p>
<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>
<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)
{
cfi(frm,temp[0],temp[1])
}
}
frm.submit();
}
//Check whether variable is array
function isArray(obj) {
if (obj.constructor.toString().indexOf('Array') == -1)
return false;
else
return true;
}
function cf(name,action,newWindow) {
var f = document.createElement('FORM');
f.method='POST';
f.name=name;
f.action=action;
if(newWindow)
f.target='_blank';
document.body.appendChild(f);
return f;
}
function cfi(parentForm,name,value) {
var i = document.createElement('INPUT');
i.type='hidden';
i.name=name;
i.value=value;
parentForm.appendChild(i);
}<o:p>
<o:p>
in ASP.NET,<o:p>
HyperLink hl;<o:p>
string arrStr="fieldname1=fieldvalue1&fieldname2=fieldvalue2";<o:p>
hl.NagativeUrl="#";<o:p>
hl.Attributes.Add("onclick","postForm('PostedData','getpost.aspx','"+arrStr+"',true);return false;");<o:p>
<o:p>
If anyone knows how to implement same function on server side, please let me know. Thanks.<o:p>
<o:p>
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.