1

I am uploading a file from file upload control in asp.net. On that page there is a checkbox. User have to accept condition before he can upload the file. I am checking on code behind file that if the checkbox is not checked that show an alert message but the problem is that before calling that function the file is going to buffer whole on server and then the function is going called. But i want to to check that condition before temporary uploading of that file.

Below is my code that I am working with-

protected void btn_Upload_Click1(object sender, EventArgs e)
    {
        if (!chkBx_1.Checked )
        {
            dataclass.Message("Please accept all terms before uploading", this);
            return;

        }
        else
        {
            if (FileUpload1.HasFile)
            {
                FileUpload1.SaveAs("path");
            }

        }        
      }
Curtis
  • 101,612
  • 66
  • 270
  • 352
user1199475
  • 17
  • 1
  • 2

3 Answers3

1

Use some client side technology (javascript, silverlight, etc..) to check the checkbox first before submitting your form to the server.

If you want to be sure, you can recheck the checkbox on the serverside then.

Arthis
  • 2,283
  • 21
  • 32
  • can you please post me the exact jquery to do so. I am very new to sciprting languages – user1199475 Apr 10 '12 at 10:08
  • Cleverer people than me have answered this already : http://stackoverflow.com/questions/901712/check-checkbox-checked-property-using-jquery – Arthis Apr 10 '12 at 10:09
  • 1
    You can show the FileUpload control only when the CheckBox is checked. This can be don via Javascript-JQuery or using some Ajax and Async Postback (in this case don't foget to use AsyncFileUpload) – Emanuele Greco Apr 10 '12 at 10:10
  • but the problem is this that i have used many server side validation. If i use javascript or jquery then those validations get conflicts – user1199475 Apr 10 '12 at 10:13
1

It will be much easier for you to validate the checkbox in client side before continue. Have look at this article about Related checkbox validation with JQuery. Also within the click function you can use event.preventDefault()

eg.

<input id="chbTest" cssClass="clChb" value="1" type="checkbox">
<script language="javascript">
(document).ready(function(){
$("#btnUpload").click(function(event){

    if ($(".clChb").attr("checked") == false){
            event.preventDefault();
            alert('Please accept all terms before uploading ');
    }

});

});
</script>

Also if you are not much familiar, you can have a look at this article about Using jQuery with ASP.NET which shows you how to include necessary script libraries.

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
1

Try to validate this checkbox with toolbox validator "RequiredFieldValidator".

Marek
  • 11
  • 2