This is a migrated thread and some comments may be shown as answers.

Preventing Postback If No File is Selected

3 Answers 204 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Rodney
Top achievements
Rank 2
Rodney asked on 30 Jun 2016, 04:22 PM

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

3 Answers, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 05 Jul 2016, 06:16 AM
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.
0
Rodney
Top achievements
Rank 2
answered on 06 Jul 2016, 11:01 AM

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.

 

 

 

0
Ivan Danchev
Telerik team
answered on 07 Jul 2016, 01:11 PM
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.
Tags
AsyncUpload
Asked by
Rodney
Top achievements
Rank 2
Answers by
Ivan Danchev
Telerik team
Rodney
Top achievements
Rank 2
Share this question
or