开发者

Make server-side validation without losing data on postback

I would like to know which is the best way to do this: I have a form using ASP that is being validated firstly on client-side with jQuery. In this form I have a FileUpload control to upload an Excel file and the validation of this control is being made on server-side to check the file type, valid data, valid structure, etc... I really need to make the file validation on server-side to be as secure as possible and I don't want to use ActiveX.

The problem is that, when the server-side validat开发者_运维知识库ion returns an error, the previously inserted data in the form is lost due to the postback.

Is there a way to make client-side validation, then after this is done, make the server-side validation and on the postback don't lose the sent data?


I think the best way to do that is to get old values of field at Page_Load. All you have to is check if page is postback.

     if (Page.IsPostBack)
    {
        YourTextBox.Attributes.Add("Value", YourTextBox.Text);
    }

Hope this help you.


Try adding a CustomValidator to you form that will call the FileUpload.SaveAs() method and that will validate the uploaded file.

For example:

protected void ValidateCstm_ServerValidate(object source, ServerValidateEventArgs args)
{

    //Upload the file
    fileUpload1.SaveAs(....
    .....
    .....

    //Validate the fileUpload
    .....

    //If the fileUpload is invalid
    args.IsValid = false;

}

I hope that this helps.


Have you considered using AJAX for the file validation, thus avoiding a full post back altogether? There are several AJAX-based file upload widgets 'out there' that might serve your purposes...


just when you encountered any error on server side , set all your form post data in some session variable and redirect to the form and show the form filled up with this session data.


Alternatively you can use jQuery File Upload component. It does not require post backs. It needs server to handle HTTP GET, POST and DELETE and return JSON. You can use ASHX for this. This way your server side validation will not interfere with post backs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜