Solved

Html.BeginForm Submit File Upload Empty alert?

Posted on 2013-06-28
7
1,387 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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