简体   繁体   English

在asp.net中验证错误后,Page.IsValid始终为false

[英]Page.IsValid is always false after validation error in asp.net

i have a code to file upload ... in onchange event checking file size , format before uploading to physical folder. 我有一个代码来文件上传...在onchange事件检查文件大小,格式上传到物理文件夹之前。 if all validation are true then Upload button will be triggered from onchange event. 如果所有验证都为真,那么将从onchange事件触发上传按钮。

Everything working fine but i upload wrong format file and validation error will popup and in next file upload Page.Isvalid is false so file not uploading even the correct format/size is uploaded. 一切正常,但我上传错误的格式文件和验证错误将弹出,并在下一个文件上传Page.Isvalid为false,所以文件不上传甚至正确的格式/大小上传。 how to solve this issue. 如何解决这个问题。

below is the code: 以下是代码:

javascript JavaScript的

 function UploadFile(fileUpload) {


    if (fileUpload.value != '') {
        var title = document.getElementById("<%=txtTitle.ClientID%>").value;
        var sFileName = fileUpload.value.toLowerCase();
        var sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1];
        var file = (fileUpload.files[0].size / 1024);
        var limit = <%= ConfigurationManager.AppSettings["MaxLimit"]%>;

        var maxlimit = limit * 1024;
        var type = document.getElementById("<%=hdnType.ClientID%>").value;
        if (title != "") {
            if (file > maxlimit) {


                fileUpload.value = null;
                ShowAlert("Sorry the file you are trying to upload exceeds the allowed file limit. Allowed size is 10MB.");

            }
            else {
                if (type == 1) {
                    if (sFileExtension != "png" && sFileExtension != "jpg" && sFileExtension != "jpeg") {

                        fileUpload.value = null;
                        ShowAlert("File Format not supported. Please upload .jpeg/.png files");


                    }
                    else {
                        document.getElementById("<%=btnSave.ClientID%>").click();

                    }
                }
}
}


 <asp:FileUpload ID="upldfile" runat="server" onchange="UploadFile(this)"  ClientIDMode="Static" />
 <asp:Button ID="btnSave" runat="server" UseSubmitBehavior="false" ValidationGroup="save" Text="Save" style="display:none;" OnClick="Upload"  data-dismiss="modal"   class="btn btn-danger" />

Upload event 上传活动

 public void Upload(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SavePortfolio();
        }


    }

Try this 尝试这个

function UploadFile(fileUpload) 
{
    if (fileUpload.value != '') {
        var title = document.getElementById("<%=txtTitle.ClientID%>").value;
        var sFileName = fileUpload.value.toLowerCase();
        var sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1];
        var file = (fileUpload.files[0].size / 1024);
        var limit = <%= ConfigurationManager.AppSettings["MaxLimit"]%>;
        var maxlimit = limit * 1024;
        var type = document.getElementById("<%=hdnType.ClientID%>").value;

        if (title != "") {
            if (file > maxlimit) {
                fileUpload.value = null;
                ShowAlert("Sorry the file you are trying to upload exceeds the allowed file limit. Allowed size is 10MB.");
                return;
            }
            else {
                if (type == 1) {
                    if (sFileExtension != "png" && sFileExtension != "jpg" && sFileExtension != "jpeg") {
                        fileUpload.value = null;
                        ShowAlert("File Format not supported. Please upload .jpeg/.png files");
                        return;
                    }
                    else {
                        document.getElementById("<%=btnSave.ClientID%>").click();
                    }
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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