views:

58

answers:

4

I'm trying to convert a classic ASP page to ASP.NET 3.5.

On the page, there is a small form to submit your e-mail address to an external newsletter site. Here's the code:

<form name="emailForm" action="http://www.site.com/emailsignup.aspx" method="get">
    <input type="text" name="email" />
    <input type="submit" id="btnSubmit" name="btnSubmit" />
</form>

I was hoping I'd just be able to drop this on the page and it would work, but it doesn't, it just reloads the page.

How am I supposed to do this? Does it need to be done in the code behind of the button's click event?

+2  A: 

In ASP.Net, by default controls - like the button - that cause postbacks will submit the page back to itself EVEN if you set the action attribute on the page to another file. What you want is called Cross-Page Posting. The following MSDN pages shows you how to do this with ASP.Net 4, but there is a link at the top to get to older versions:

http://msdn.microsoft.com/en-us/library/ms178140.aspx

http://msdn.microsoft.com/en-us/library/ms178139.aspx

Otherwise you can just use the Button's Click Event Handler in the code behind.

Hope this helps.

Waleed Al-Balooshi
I'd suspect this to only be valid for the form all controls are usually nested in. Adding a different form (outside of the one used by ASP.NET) would not have those restrictions. Do you know?
Rune FS
I am assuming you mean adding an additional form without the runat="server", because you can't have more than one server side form in a page. If that is what you mean then you are correct that it wouldn't postback, but you wouldn't be able to use server side controls or the viewstate with normal forms. The problem is that I believe the OP has inserted the html form inside the server form, which is not recommended. Based on the result, it seems that the outter server form is taking over and posting back.
Waleed Al-Balooshi
A: 

you must be missing runat="server" in the form tag

try to create a page through the IDE and paste the code for input tags between the form tags

<input type="text" name="email" />
<input type="submit" id="btnSubmit" name="btnSubmit" />
Rony
Why would runat="server" change what's happening on the _client_ side. The post back of the form hits the wrong server. I.e. the one the page came from and not the one specified in the action attribute.
Rune FS
A: 

It surely is ending nested in the form aspx get by default.

Depending on the layout of your page, you can modify it so you don't end with a nested form. If that's not possible, I think you can't get around to use a form, so instead you'll have to look at a different solution like building the get with js.

eglasius
A: 

The easiest way would be to put that <form> tag outside the main <form runat="server"> tag that usually wraps all ASP.NET controls.

If you're using a master page and the only content placeholder you can use is within that <form runat="server" tag, or you need this form tag in the page structure within the main <form runat="server"> tag then you need to:

Take out the simple <form> tag but leave the HTML <input> tags. Handle the client onclick event (the JavaScript versions, not ASP.NET postback handlers) of the submit button. That handler should grab the e-mail from the text box and issue something like window.location = 'http://www.site.com/emailsignup.aspx?email=....' in Javascript. Make sure to cancel the default HTML button action handler so it doesn't bubble up and submit the ASP.NET form too.

Mufasa