Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried my level best to adopt a required field validator for my FileUpload. It is not working. Can anyone guide me please.
XML
--%><asp:Panel ID="Panel117" runat="server"
        style="border-width: 1px; border-style: solid; top: 400px; left: 1185px; position: absolute; height: 161px; width: 279px; background-color:InactiveBorder; border-color:Black">

            <asp:Image ID="ImagePreview" runat="server" Height="97px" Width="99px"
            style="z-index: 1"/>
            <asp:FileUpload ID="PhotoUpload" runat="server"
              style="z-index: 1; left:100px; top: 53px; position: absolute; height: 46px; width: 218px; right: 90px;" />
           <asp:Button ID="Button7" runat="server" CausesValidation="False"
            onclick="Showbutton_Click"
            style="z-index: 1; left: 7px; top: 123px; position: absolute"
            Text="Show" />

</asp:Panel>

<%--</ContentTemplate>--%>
<Triggers>
<%--<asp:AsyncPostBackTrigger ControlID="Button3" EventName="Click" />--%>
<asp:PostBackTrigger ControlID="Button2" />
</Triggers>
<%--</asp:UpdatePanel>--%>


    <asp:requiredfieldvalidator id="rfvWeeklyCommentary" runat="server" controltovalidate="PhotoUpload"
 errormessage="Select a foto."
setfocusonerror="true">
</asp:requiredfieldvalidator>
Posted

Input (File) is an HTML control, so you will need to run it as a server control and then validate it on the code-behind. If you don't want to set it a server control, you can validate it using JavaScript/jQuery in the client-side.
For instance,
JavaScript
var fileUpload = $("*[id$='fileUpload ']").val();
if(fileUpload == ""){
//Do your validation here
}
 
Share this answer
 
Hi...
see this may helpful to u.

C#
             if (!fileupload.HasFile)
            {
                lbl.Visible = true;
                lbl.ForeColor = Color.Red;
                lbl.Text = "Please Select file";    //checking if file uploader has no file selected
            }
            else
            {
                //Continue
                // if file is selected. 
            }

thank u.
 
Share this answer
 
Required filed validator wont work
check whether it has file or not using
C#
FileUpload1.HasFile

or
C#
!(FileUpload1.PostedFile.ContentLength>0)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900