Submitting form to an external site in .NET 2.0
Because .NET disallows form tags (other than its own) on the page, creating a form that submits to another external site (say a login) is tricky. The way to do it is to inject a form into a literal on the page, and then put in some javascript to "submit" it.
Here is what I put on my masterpage, outside the form tag:
<asp:Literal ID="ctlLoginForm" runat="server"></asp:Literal>
<asp:Literal ID="ctlLoginScript" runat="server"></asp:Literal>
Then on the click of the button in the code-behind:
((Literal)Page.Master.FindControl("ctlLoginForm")).Text = "<form id=\"_xclick\" name=\"_xclick\" action=\"http://www.site.com//login.asp\" method=\"post\"> <input type=\"hidden\" name=\"login\" value=\"" + login.Text + "\"><input type=\"hidden\" name=\"password\" value=\"" + pass.Text + "\"></form>";
((Literal)Page.Master.FindControl("ctlLoginScript")).Text = "<script language='javascript'>var ctlForm = document.forms[1];ctlForm.submit();</script>";
Of course use the url of the site you are "submitting" to. Note that there already is the default form on this page, so the use of forms[1]. I've run into problems doing this inside a Telerik ajax panel.
Here is what I put on my masterpage, outside the form tag:
<asp:Literal ID="ctlLoginForm" runat="server"></asp:Literal>
<asp:Literal ID="ctlLoginScript" runat="server"></asp:Literal>
Then on the click of the button in the code-behind:
((Literal)Page.Master.FindControl("ctlLoginForm")).Text = "<form id=\"_xclick\" name=\"_xclick\" action=\"http://www.site.com//login.asp\" method=\"post\"> <input type=\"hidden\" name=\"login\" value=\"" + login.Text + "\"><input type=\"hidden\" name=\"password\" value=\"" + pass.Text + "\"></form>";
((Literal)Page.Master.FindControl("ctlLoginScript")).Text = "<script language='javascript'>var ctlForm = document.forms[1];ctlForm.submit();</script>";
Of course use the url of the site you are "submitting" to. Note that there already is the default form on this page, so the use of forms[1]. I've run into problems doing this inside a Telerik ajax panel.

1 Comments:
You're a lifesaver dude!
Post a Comment
Subscribe to Post Comments [Atom]
<< Home