tags:

views:

521

answers:

4

I have an html page that I am converting over to an asp .net page. This page contained a form that accesses an external website that I have no control over. There is some sample code below:

<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
<form name="Subscribe" method="post" action="http://anexternalwebsitehere.com/subscribe.asp"&gt;
        <input type="text" name="email" size="45" maxlength="120" />
        <input type="submit" name="submit" value="Subscribe" />
    </form>
</asp:Content>

The form is more complicated than the example I have provided, but it gives a rough idea of what i need to convert over. Here are the problems I have encountered.

If I leave it as is: When you click on the submit button you have a postback to the current page and not to the external page

If simply convert everything over to be asp form controls and change the postback url: The id's become some convoluted "ctl00_body_ctl00" which the external page is not able to interpret.

Note: I do need the page to be an aspx page because I am using a master page for other content on the page.

Additional note: this is not Microsoft MVC.

What am I missing?

+2  A: 

Since you probably have the server form tag on your masterpage spanning your contentplaceholder, this new form you're declaring will be placed inside the server-form (by server-form i mean the one asp.net use for postbacks with runat="server")

I've had cases when i needed a special non-server form on an aspx page that already had a server-form, and the way i solved the problem was to place this non-server form outside the server-form - what i mean is, place it after the server-form. Since you use masterpages, you will need a new contentplaceholder on that masterpage, you can call it "noform". It is placed after the server-form so any content put in this noform will be placed outside the server-form. This mean no asp.net controls will work in this specific contentplaceholder (noform) since they won't be picked up by the framework, but you will be able to place your non-server form there and do your magic on that.

Per Hornshøj-Schierbeck
A: 

The problem, as you've probably guessed, is that you've got one form inside another form - ie the legacy form is appearing inside the ASP.NET form required by the master page.

One quick (if rather clunky) way to get around this is to close the ASP.NET form above the legacy form, and then open a new form below the legacy form. This means you've got three forms on the page, none of which are nested.

So you end up with something like this:

<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
    </form>
    <form name="Subscribe" method="post" action="http://anexternalwebsitehere.com/subscribe.asp"&gt;
        <input type="text" name="email" size="45" maxlength="120" />
        <input type="submit" name="submit" value="Subscribe" />
    </form>
    <form method="post" action="myAspNetPage.aspx">
</asp:Content>

The closing </form> tag at the start closes the ASP.NET from the master page. You then have your form, which should now work as expected. Then the open <form> tag at the end simply ensures that the closing </form> tag from the master page is valid HTML.

Obviously anything appearing on the master page after the legacy form won't be within the standard ASP.NET form, so this may not work for you depending on how the rest of your page is structured.

It's not a particularly elegant solution, but it works as a quick fix (depending on what else is on your master page). We've used it where we had one legacy form required on a site with hundreds of pages, so we simply wanted a one-off fix rather than anything that affected the master page itself.

In our case, we couldn't change the legacy form as this was supplied by a third-party, regularly changed, and needed to be dropped into the ASP.NET page without a developer getting involved to amend it (eg as opposed to Brian's solution to his own question which requires editing the form and is clearly a better option in his case - and probably in most other cases where there is a similar problem).

Simon Forrest
A: 

Your button's click event will handle submission of the url and data.

//C# source
protected void button_Click(object sender, EventArgs e)
}
  string customURL = "http://anexternalwebsitehere.com/";
  string emailValue = textBoxEmail.Text; //of course validate this for proper email...

  customURL += "page.aspx?email=" + emailValue;

  Response.Redirect(customURL);
}
Byrdsong
This will not work as it needs to be a post, not a get submit.
Brian
+3  A: 

The issue was with nested forms as others have mentioned.

I was able to fix all my issues by simply doing the following:

  1. Remove the extra form element i was adding.
  2. Leave all controls as simply html controls, except for the submit button.
  3. Replace the submit button with an asp .net button, and set the postback url.

The old code is as follows:

<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
<form name="Subscribe" method="post" action="http://anexternalwebsitehere.com/subscribe.asp"&gt;
        <input type="text" name="email" size="45" maxlength="120" />
        <input type="submit" name="submit" value="Subscribe" />
    </form>
</asp:Content>

The new code:

<asp:Content ID="sample" ContentPlaceHolderID="body" Runat="Server">
        <input type="text" name="email" size="45" maxlength="120" />
        <input type="submit" name="submit" value="Subscribe" />
        <asp:button postbackurl="http://anexternalwebsitehere.com/subscribe.asp" text="Subscribe" runat="server" />
</asp:Content>

This fixes any of the issues with invalid nested forms as there are none. It also addresses the issue of asp .net renaming the asp elements because the only control that is being renamed is the asp button control which was not necessary for the submission to succeed.

Brian