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?
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.
Related
I have a Website and in one webpage there is a form, where one of the fields is to upload an image. I have created in my database a column picture of type LONGBINARY. I have read some webpages where they explain how to use <asp:FileUpload ID="FileUpload1" runat="server"/>. But the doubt is that I have an input tag <input id="Submit1" type="submit" value="Put In Store" /> for my form. All the webpages where they explain about FileUpload are using a ASP button, but for my form I need an submit input.
So:
What do I do if it says I need a ASP Button but I need a submit input?
Where need to be the code of the FileUpload when you click?
My last question about FileUpload(There are the links they gave me).
You can upload a file using your way. Just in the form tag, do mention the type attribute type as multipart data. So when you click on Submit the request goes to the place that is mentioned in the action attribute of the form tag.
Example form tag:
<form action="demo_post_enctype.asp" method="post" enctype="multipart/form-data">
In my asp.net web control form i am using two text box 1st is simple input html control and 2nd is asp.net input web control.
<form id="form1" runat="server">
Email: <input type="text" id="txt_email" name="txt_email" value="" /><br />
Email2: <asp:TextBox ID="txt_email2" runat="server"></asp:TextBox><br />
<asp:Button ID="btn_login" Name="btn_login" runat="server" Text="Button"
onclick="btn_login_Click" />
</form>
I need to know what is the difference using simple control and asp.net input control both of them pass the value to code behind after the form submit. can any one help me on this?
As defined in your example input type="text" won't even be visible to code-behind because it is missing runat="server" attribute.
If you do add it - there're still differences. ASP.NET TextBox is more advanced and in par with the rest of ASP.NET model (e.g. it has property .Text vs. .Value of an HtmlInput control, it has events and other properties).
But if you simple need to pass text information back to the server, either of them will do the job.
The biggest differences are that
the asp.net controls are rendered on the server, and thus they have more overhead on your server than using traditional controls - traditional controls (by default) are rendered once then basically reside on the client's browser, asp controls are persistent on the server side.
the asp controls can be accessed and worked with directly in the code behind files.
asp controls have some additional tags that can be used on their fields usually.
as was pointed out by #Yuriy-Galanter, how the value is accessed is slightly different.
The asp:Textbox renders HTML to the client/browser when the page request is made. Picture the ASP.NET control, in this case an asp:TextBox as a server side bit of code that knows to render a <input type="text"> HTML element when the request for the aspx page is made to the server.
The ASP.NET compiler, when parsing your aspx page just spits out the <input type="text"> HTML element you have for Email: and for Email2: the ASP.NET compiler knows that is a server control because of the runat="server" tag. So the ASP.NET compiler, having a reference to the ASP.NET assemblies on the server, reads the code for the <asp:TextBox> and knows to ultimately respond to the page request with an <input type="text" id="txt_email2" />
The server side controls are accessible in your code behind page. So is accessible in the code behind but the <input> element is not. Good for you to consider in your research at this point, that if you add runat="server" to your element, it is accessible in your code behind.
I'm using umbraco with extensionless urls.
I've inserted a simple piece of HTML in one of my masterpages (en/test) :
<form method="post" enctype="multipart/form-data">
<input type="submit" />
</form>
When I press the submit button, I get a 404. The path is exactly the same and should exist.
When I remove the enctype part, the submit occurs fine.
I can't figure out how to fix this, but I bet it has something to do with the rewriting.
I also tried the following without success:
<form method="post" enctype="multipart/form-data" action="/en/test">
<input type="submit" />
</form>
<form method="post" enctype="multipart/form-data" action="/en/test.aspx">
<input type="submit" />
</form>
The only page where I CAN use the enctype attribute, is on the actual homepage. I guess this has to do with the fact that the physic default.aspx exists.
=============== UPDATE =================
There is only one form element in the page, the one that I've inserted. So a "whole page" form element is certainly not the case. Secondly, yes the form is in theory posting back to itself. I also tried an empty action tag, plus an action tag with the full url as suggested, with the same results.
When I either use the following scenario's:
No action attribute
action=""
action="{relative path}"
action="{absolute path}"
I end up on exact same URL as where I fired the submit from. But it's a 404. When I press the enter key in my address bar, no 404, I'm back at my original page with the same URL.
First question I should ask is do you get a 404 when you browse to "/en/test" or "/en/test.aspx". For the form to post back to itself try an empty action e.g. action="" or writing the current url into the action attribute. And one further question, do you have another form wrapped around your page with the runat="server" attribute because if you have you will end up with nested forms which will also cause you issues.
On a side node I would strongly suggest upgrading your installation to at least v4.11.4 due to a bug that was introduced in 4.10. Please see the following for details...
Trying to publish a root node (parent) after unpublishing a child result in a YSOD:
http://issues.umbraco.org/issue/U4-1491
Path Fixup
http://our.umbraco.org/projects/developer-tools/path-fixup
I am doing following in one of my webpart .ascx file.
<FORM action="https://illustration.sagicorlifeusa.com/fse5/main/FormPost.aspx" id="frmLogin" method="post" target=blank>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<%= CompleteRequest %>
<input type="submit" name="__exclude__Submit" value="Run Sagicor Life Illustration Software Online" />
Notice the method="post"; However, looks like when I add this web-part, The hosting page already has a <FORM/> . How can I do the above POST?
posting to a new window from inside an ASPX web form (which SharePoint is) is tricky, because the framework wraps everything in a <form> it keeps track of. If you are required to post to the target page (i.e. get isn't good enough), you might have to:
create a separate .aspx page entirely to produce the form
open that form from within your Web Part with a JS call to window.open or an <a href="..." target="blank"
auto-post that form, with JavaScript, as soon as it loads.
(I have no firsthand experience with sharepoint; take this answer with a grain of salt.)
I'm writing an app with ASP.NET MVC where, for various reasons, the navigation is handled through HTML input buttons. What are the Best practices on how to handle this situation?
Set up a Form on the buttons where information needs to be POSTed and just use JavaScript to redirect on the buttons where information doesn't need to be retained
Have all buttons handled through forms, to the point where a mini-form handles navigation on the buttons where the information doesn't need to be retained
<% using (Html.BeginForm())
{ %>
<input type="hidden" name="controller" value="1" />
<input type="hidden" name="action" value="Location" />
<input id="BackButton" type="submit" value="Go Back" />
<% } %>
Something I haven't thought of here
Here you can find a pretty good article on how to style an anchor tag like a button
Javascript is probably your best bet.
Using a mini form seems really heavy handed. You are basically looking to use a button as a link (I'd look seriously at why you are breaking that very basic web convention: buttons post/modify and links navigate) so the more basic you can make it the better.
3: Style some anchor tags to look like buttons.
On a recent application we implemented option 1. The client liked having "Cancel" buttons on all the forms (...) so we just stuck a click event on the reset inputs which did:
history.go(-1); return false;
Use empty <a href="#"> tags and Javascript/jQuery. For example:
In View:
<script src="<%= Url.Content("~/Content/navigation.js") %>" type="text/javascript"></script>
...
<a id="back" href="#">Back</a>
<a id="next" href="#">Next</a>
<script type='text/javascript'>
var back_href="<%= Url.Action("...") %>";
var next_href="<%= Url.Action("...") %>";
</script>
Somewhere in navigation.js:
$(document).ready(function() {
$('a#back').attr('href', back_href);
$('a#next').attr('href', next_href);
});