Solved

Html.BeginForm Submit File Upload Empty alert?

Posted on 2013-06-28
7
1,402 Views
Last Modified: 2013-07-01
This work fine for ASP:Button, but I'm working with MVC.

- How do I code alert box if file upload is empty

- asp:button -
<asp:FileUpload ID="fileImageUrl" ClientIDMode="Static" runat="server" size="40" />

<asp:Button ID="btnUpload" 
         ClientIDMode="Static" 
         Text="Upload Picture" 
         runat="server" 
         OnClientClick="return validateUpload();" 
         onclick="btnUpload_Click" />

<script type="text/javascript">
        function validateUpload() {
            if (document.getElementById("fileImageUrl").value == "") {
                alert("Please select an image to upload.");
                return false;
            }
            return true;
        }
    </script>

Open in new window


- MVC -
@using (Html.BeginForm("Upload", "FileManager", null, FormMethod.Post, 
               new { enctype = "multipart/form-data", @class = "myform" }))
        {
            <label>File Upload</label>
            <input type="file" name="filename" style="padding: 2px 5px"/>  
            <br />   
            <br />       
            <input type="submit" value="Upload File" style="padding: 2px 5px" />

Open in new window

0
Comment
Question by:WorknHardr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39285825
Have you tried adding in your Javascript?

e.g.

@using (Html.BeginForm("Upload", "FileManager", null, FormMethod.Post, 
               new { enctype = "multipart/form-data", @class = "myform" }))
        {
            <label>File Upload</label>
            <input type="file" name="filename" style="padding: 2px 5px"/>  
            <br />   
            <br />       
            <input type="submit" value="Upload File" style="padding: 2px 5px" onclick="return validateUpload();" />

<script type="text/javascript">
        function validateUpload() {
            if (document.getElementById("fileImageUrl").value == "") {
                alert("Please select an image to upload.");
                return false;
            }
            return true;
        }
    </script>

Open in new window

0
 

Author Comment

by:WorknHardr
ID: 39285941
I wasn't sure if 'OnClick' would work like that, I'll let you know...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39285976
onclick is standard Javascript. OnClientClick is just special notation for ASP.NET Web Forms. It is output as an onclick on the rendered tag.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:WorknHardr
ID: 39286014
Okay
0
 

Author Comment

by:WorknHardr
ID: 39289897
Here is working code:

@using (Html.BeginForm("Upload", "FileManager", null, FormMethod.Post, 
               new { enctype = "multipart/form-data", @class = "myform" }))
        {
            <label>File Upload</label>
            <input type="file" id="filename" name="filename" style="padding: 2px 5px"/>  
            <br />   
            <br />       
            <input type="submit" value="Upload File" style="padding: 2px 5px" 
              onclick="return validateUpload();" />
}

 <script type="text/javascript">
            function validateUpload()
            {
                if (!$("filename").val())                     
                {
                    alert("Please select an image to upload.");
                    return false;
                }
                return true;
            }
        </script>
        }
    </script>

Open in new window

0
 

Author Closing Comment

by:WorknHardr
ID: 39289899
danka
0
 

Author Comment

by:WorknHardr
ID: 39289976
Help, spoke to soon. The Alert opens no matter if Input is empty or not!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question