Reputation: 1536
I am having problem with overriding Form Action URL
I have a class to override the Form Action.
<html>
<head>
</head>
<body>
<form id="form1" runat="Server" action="https://www.test.com">
<div style="">
<asp:button runat="Server" onclick="PostMe" Text="Post"/>
</div>
<asp:HiddenField runat="server" ID="test" value="0" />
<asp:HiddenField runat="server" ID="test2" value="0" />
</form>
</body>
</html>
<script runat="server">
void PostMe(Object sender,EventArgs e){
RemotePost2 myremotepost = new RemotePost2();
myremotepost.Url = "https://www.test.com";
myremotepost.Add("field1","Tom");
myremotepost.Add("field2","Sawyer");
myremotepost.Post();
}
public class RemotePost2{
private System.Collections.Specialized.NameValueCollection Inputs = new System.Collections.Specialized.NameValueCollection();
public string Url = "";
public string Method = "post";
public string FormName = "forms";
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();
}
}
The problem is I cannot post field1 and field2 instead the form posts test and test2 values. When I use postBackUrl attribute it still posts test and test2.
What I want to do is to post field1 and field2 when the form action is not blank. Because when there is no action it works fine and I cannot modify the action on the code.
Your help will be much appreciated Cheers
Upvotes: 0
Views: 2020
Reputation: 9942
You can add below to the code behind on load function
myButton.Attributes.Add("onClick", "javascript: document.forms[0].action='http://otherserver.com/theirpage.html';");
where myButton will be id of your button.
Upvotes: 3