How not to upload file on submit - asp.net

I have a form in asp.net with a FileUpload control inside. Next to this control I have "Upload" button which is used to upload a file to a list of files. The problem is that I also have "Submi"t button used for submiting whole form.
Now when somebody selects a file through browse button and presses on "Submit" not "Upload" file is being uploaded and I wouldn't want that.
I'm hoping for asp.net or general HTML answer

Sounds like the FileUpload control and Upload button should be inside it's own form. With ASP.NET and the 1 server-side form restriction, you may have to resort to regular HTML controls to do this.
Alternatively, you may be able to use JavaScript to clear the FileUpload if Upload isn't clicked. Browsers can be picky about access to file inputs, though (for security reasons) so it may not be accessible.

For the general HTML answer, have your file upload button be part of a separate form from the other form, meaning they are encapsulated within separate Form tags.
For ASP.NET, you're going to have more pain, since ASP.NET by default has only one form per page.
Ultimately, however, I think you're better off rethinking your design. Try to put that file upload on a separate page; if you have a page that does more than one thing, you're inviting confusion on the part of the user.

Submit button will send all of the form's information.
Try putting the FileUpload control outside, on a diferent form.

Your FileUpload control needs to be in its own form if you want to to be submitted separately. When you click the Upload button, the other parts of your form are being submitted as well. That's just how it works.

I would separate the two forms. Using JavaScript or something like that could get to be a pain. The problem with separating the forms is you have to do it in a way where you don't lose your original form, meaning if you have other form information around the file upload you might have a bit of a nightmare on your hands but if you can put the file upload either above or below the form that submit controls then it's easy.
<form id="A">
<button name="submit>
</form>
<form id="B">
<fileupload>
</form>
I use this method on one of my sites and it works quite well.

Related

Adding lightbox (or something similar) to ASP.NET project

I need to implement some, let's call it "dialog" in some old ASP.NET project that I took over. It's huge project so I'm not allowed to implement new things.
I have a form on which user can attach up to 3 file attachments, enter some 10-15 fields, and when required fields are filled, submit button get's enabled.
What need to be done is press submit, wait for any possible response for server, and then display some sort of dialog, alert, lightbox which would display short summary and list of correctly uploaded files.
What would be the best approach to do this in ASP.NET?
Considering jQuery Lightbox is designed for overlaying images rather than HTML I would recommend using FancyBox or FaceBox instead.

ASP.NET login form that works well with browsers 'remember password' feature

I know this has been asked several times, but I'm not happy with the answers so far.
Root problem is the way the browsers detect a "login form" to auto-fill the fields and decide if they should ask the user to save the info. It works fine if your form has just the basic login fields. If the same form has lots of other fields, the browser doesn't detect it as a login form.
The solutions I've seen say to use a separate non-ASPX form on your page. In my case all of my input fields are rather complex subclassed versions of ASPX controls, mostly to get stuff like a custom background image and nice handling of place-holder/watermark text and items of that nature. So trying to create a non-ASPX form is a lot more work since I can't use these controls on the form. I'd have to replicate them in standard HTML. I could do this, but would rather not. Plus it's more maintenance anytime I need to make a change.
Is there a better way? That lets me use all of the greatness that is ASP.NET.
My page has both a login area and a register area (name, email, DOB, etc). This is why the browsers are confused.
I seem to recall when I was writing a website for mobile browsers that there was some markup that could be used to tell the browser "this is a login name field, and this is a password field". If something like that was available, that would be perfect.
Maybe I could do something with an iframe? Like an ASPX page inside an ASPX page?
Despite the complexity of your code, any custom ASP.NET user controls will eventually render output as HTML and every browsers only speak HTML
What you 're looking for is how to write ASP.NET code that results HTML mark-up containing the simple attribute..
<input type="password" autocomplet="off" ...
So that all browser will able to recognize it's a form containing password field. It doesn't care what you do in code behind. Focus on the output.

How to upload file with form-post using c++ Qt-WebKit?

So, when i was dealing with forms before what i would do is find web element that is part of the form input, set value of that form input by element.setattribute("value", "infoi want to add")
then simply when i set value of form elements i click submit button of form.
Is this possible to do with web-form input type="file" and how can i do it?
Thanks
The simplest and the most straightforward way to do exactly what you've described is to get form's target address with QWebFrame::hitTestContent and then to post a file with QNetworkAccessManager::post. You also could inject some javascript code into a loaded page (see this for more details), but that would be an overkill for such a simple taks. And besides in the latter case all your injected javascript will be gone after submitting the form (i.e. after the page is reloaded).

asp.net mvc Upload File ajax

Hi ive got an mvc form with a fileupload functionality. Ive got an action that accepts an file and extracts thumbnails from it, after which the user can select the images and then proceed to submit the form. How can post the initial file via ajax, bearing in mind, this is not the final submission on the form and I want to retain user input. ie no postback
Thanks
I use the ajaxupload plugin for jQuery. Lots of sample code is provided on the site. From the site:
[The] plugin creates invisible file input on top of the button you provide, so when user clicks on your button the normal file selection window is shown. And after user selects a file, plugin submits form that contains file input to an iframe. So it isn’t true ajax upload, but brings same user experience.
Browsers don't allow the uploading of files via ajax. There are several good workarounds, however.

Asp.Net nested form

I need to supply an html form (not a server form) for searching within an asp.net web-forms website. The form will post to another website where an indexed search is performed. Seeing as nested forms don't work well in asp.net, what is the simplest approach for this?
The nested form is a simple html form that performs a "get" against the search website, which I do not have programmatic control over.
Update: I resolved my issue by simply moving the server form to the appropriate place on the page, rather than having it surround the entire page. However, I'm still wondering how this would be handled if the html form needed to be placed physically between server controls (which require the server form).
However, I'm still wondering how this would be handled if the html form needed to be placed physically between server controls (which require the server form).
You can place controls on your page without requiring an HtmlForm.
In your case there's no issue declaring another form markup, but you could also just use some search control on your main form and make it issue a GET to that website.
Not only do nested forms "not work well," you basically can't have >1 form per page at all. The simplest approach is the approach you are forced to go with: write a page that only uses one <form runat="server"></form>. Since you need search functionality, is there no ASP.NET search box control that you could use?
Have a read here.
There are 4 workarounds:
Use an IFRAME
Force Submission to Navigate Using a GET Request
Dynamically Change the Form Action
Use a 3rd Party Form Handler
More details on http://www.revindex.com/Blogs/tabid/65/EntryID/21/Default.aspx
Nested forms don't work well in HTML full stop! You should never do it.
Perhaps you mean more than one form on page? Whilst it's true you can only have one form with runat="server", I can't see any reason why you couldn't have a standard form (not server form) that posted to another site at the same level (ie. not nested).
Try adding your HTML input elements to wherever you want the nested form to be. Then use JQuery to change the page form action to point to the external Website. The entire form is submitted, but with a different external Url. The minor downside is the values for all input elements on the page are posted, but most times that is not big deal.
(I only tried this using a POST, not a GET. This is basically Roman O's #3 workaround in more detail)
<div id="nested-form">
<input type="text" name="q">
<input name="searchbtn" value="Go" type="submit" class="search-button">
</div>
<script>
$(function() {
$("input.search-button").click(function() {
$('form').get(0).setAttribute('action', 'http://external.com');
});
});
</script>
maybe you try Server.Transfer() to your target page that do the search from a button for example!

Resources