开发者

Form GET in ASP.NET

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开发者_Python百科 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?


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.


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" />


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜