Preventing Postback If No File is Selected

4 posts, 0 answers
  1. Rodney
    Rodney avatar
    40 posts
    Member since:
    Mar 2015

    Posted 30 Jun Link to this post

    I think I'm doing something wrong here. I have a RadAsyncUpload control and a submit button on the page to cause the postback. All I want to do is prevent the postback if no file has been selected, or have the submit button disabled until a file is selected. If it makes any difference this is all inside of a RadWindow.

    Here's the markup for the RadAsyncUpload

    <div class="formRow" id="divAttachFile" runat="server" style="height: 60px;">
         <div class="floatLeft">
             <label for="asyncFlUpldAttachment"><span id="spnUploadFileMark" runat="server" class="validator">*</span> Upload File: </label>
             <telerik:RadAsyncUpload ID="RadAsyncUpload1" runat="server" MaxFileSize='<%$ AppSettings:MAXATTCHFILESIZE %>'
                 MultipleFileSelection="Disabled" MaxFileInputsCount="1" AllowedFileExtensions='<%$ AppSettings:ATTCHFILEEXTNS %>'
                 EnableInlineProgress="true" TemporaryFileExpiration="36000"
                 OnClientValidationFailed="validationFailed"
                 OnClientFileUploading="fileUploading"
                 OnClientFileUploadFailed="fileUploadFailed"
                 PostbackTriggers="lnkBtnAttachmentCancel"
                 TabIndex="100">
             </telerik:RadAsyncUpload>
             <span id="ErrorHolder" style="display: none" class="validator"></span>
         </div>
     </div>

    Here's the current JavaSrcipts, pretty much straight from your samples

    <script type="text/javascript">
        var pageIsValid = true;
        function validationFailed(sender, args) {
            $("#ErrorHolder").show();
            var fileExtention = args.get_fileName().substring(args.get_fileName().lastIndexOf('.') + 1, args.get_fileName().length);
            if (args.get_fileName().lastIndexOf('.') != -1) {//this checks if the extension is correct
                if (sender.get_allowedFileExtensions().indexOf(fileExtention) == -1) {
                    $("#ErrorHolder").html("Wrong Extension! Verify file.");
                }
                else {
                    $("#ErrorHolder").html("The file size exceeds the limit");
                }
            }
            else {
                $("#ErrorHolder").html("Wrong Extension! Verify file");
            }
            sender.deleteFileInputAt(0);
        }
     
        function fileUploading(sender, args) {
            $("#ErrorHolder").hide();
        }
     
        function fileUploadFailed(sender, args) {
            $("#ErrorHolder").show();
            $("#ErrorHolder").html("File upload failed for:" + args.get_fileName());
            sender.deleteFileInputAt(0);
        }
    </script>

     

    Thanks

    Rodney

  2. Ivan Danchev
    Admin
    Ivan Danchev avatar
    834 posts

    Posted 05 Jul Link to this post

    Hello Rodney,

    Let's say you have the following button, which initiates postback: 
    <telerik:RadButton runat="server" ID="RadButton1" Text="Submit" AutoPostBack="true" OnClientClicking="OnClientClicking"></telerik:RadButton>

    You can prevent the postback by canceling the click event in the OnClientClicking event handler if no file is selected in the AsyncUpload:
    function OnClientClicking(sender, args) {
        var upload = $find("<%=RadAsyncUpload1.ClientID%>");
        var files = upload.getUploadedFiles();
     
        if (files == 0) {
            args.set_cancel(true);
        }
    }


    Regards,
    Ivan Danchev
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rodney
    Rodney avatar
    40 posts
    Member since:
    Mar 2015

    Posted 06 Jul in reply to Ivan Danchev Link to this post

    Well it sort of worked. I now don't have a problem when I don't have an attachment, but if I do when I hit my codebehind to process it, the attachment isn't there and I get an index out of range referencing the asyncuploadcontrol. I litterally swapped your button for mine and now it won't upload. 

    Here's my original code:

    <asp:LinkButton ID="lnkBtnSaveAttachment" runat="server" OnClick="lnkBtnSaveAttachment_Click"
        CssClass="silverButton" ValidationGroup="attachmentValidations">

    Here's the new button:

    <telerik:RadButton ID="rbtSaveAttachment" runat="server" ValidationGroup="attachmentValidations"
        OnClientClicking="OnClientClicking" Text="Save" OnClick="rbtSaveAttachment_Click"></telerik:RadButton>

    Here's the codebehind for the OnClick event:

    attach.FileTypeCode = StringUtilities.GetFileExtension(RadAsyncUpload1.UploadedFiles[0].GetExtension());

    Aside from switching the button with the JavaScript above there is no other change and this line fails everytime. I switch back to the ASP button without the JavaScript and it works other than it doesn't validate the existence of the file.

     

     

     

  5. Ivan Danchev
    Admin
    Ivan Danchev avatar
    834 posts

    Posted 07 Jul Link to this post

    Hello Rodney,

    You have set the lnkBtnAttachmentCancel button as a postback trigger (PostbackTriggers = "lnkBtnAttachmentCancel"). When you add the RadButton, which has a different ID, and initiate a postback with it the selected file will not be uploaded (the FileUploaded event will not fire), because the postback is not initiated by a control set as postback trigger. Try setting the RadButton's ID to the PostbackTriggers property.

    Regards,
    Ivan Danchev
    Telerik by Progress
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top