Solved

Html.BeginForm Submit File Upload Empty alert?

Posted on 2013-06-28
7
1,378 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
I wasn't sure if 'OnClick' would work like that, I'll let you know...
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:WorknHardr
Comment Utility
Okay
0
 

Author Comment

by:WorknHardr
Comment Utility
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
Comment Utility
danka
0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now