简体   繁体   中英

Custom Validation if FileUpload is Empty my message is show

This is my `.aspx` for `FileuUpload` ..
<asp:FileUpload  ID="FileUpload1"  runat="server" class="multi form-control"   /> 
<asp:CustomValidator ID="ValidatorUpload" runat="server" ErrorMessage="File Upload Tidak boleh kosong" ControlToValidate="FileUpload1" Display="Dynamic" OnServerValidate="ValidatorUpload_ServerValidate"></asp:CustomValidator>

And this my .aspx.cs (Code behind).

protected void ValidatorUpload_ServerValidate(object source, ServerValidateEventArgs args)
    {
        FileUpload Upload = (FileUpload)FormView1.FindControl("FileUpload1");
        HttpPostedFile hpf = Upload.PostedFile;
        if (((CustomControls_DdlLocation)FormView1.Controls[0].FindControl("ddl_location1")).SelectedText.ToLower().Trim() == "kelanis")
        {

            if (hpf.FileName == null)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;

            }

        }

    }

I want to show message if FileUpload is Empty but my code isn't working. Need a Solution to this.

I have used this one. Works perfactly

 <asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <asp:requiredfieldvalidator errormessage="Required" controltovalidate="FileUpload1"
                                runat="server" display="Dynamic" forecolor="Red" />
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ValidationExpression="([a-zA-Z0-9\s_\\.\-:])+(.doc|.docx|.pdf)$"
                                    ControlToValidate="FileUpload1" runat="server" ForeColor="Red" ErrorMessage="Please select a valid Word or PDF File file."
                                    Display="Dynamic" />
    <br />
    <asp:button text="Submit" runat="server" />

For Images you can use the following regular expression validator

([a-zA-Z0-9\s_\\.\-:])+(.png|.jpg|.gif)$

Thanks

You should try this

if (hpf.HasFile == false)
{
    if ( hpf.FileName != "")
    {
        args.IsValid = false;
    }
}

Please use try this

protected void checkfilesize(object source, ServerValidateEventArgs args)
    {
        string data = args.Value;
        args.IsValid = false;
        double filesize = FileUpload1.FileContent.Length;
        if (filesize > 5000)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

you can try this solution

 <asp:RequiredFieldValidator ID="rfvDocument" runat="server" ErrorMessage="Upload your file!" ValidationGroup="NewDocument" ControlToValidate="fuDocument" Display="Dynamic"></asp:RequiredFieldValidator> <asp:FileUpload ID="fuDocument" runat="server" /> <asp:Button ID="btnUpload" runat="server" Text="Upload File" ValidationGroup="NewDocument" /> 

and in server code remember to put in button code that will start to save the file this:

    Me.Validate("NewDocument");
    If (Me.IsValid) { 
     //your code
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM