Reputation: 17
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");
}
}
}
Upvotes: 1
Views: 435
Reputation: 14470
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.
Upvotes: 1
Reputation: 11
Try to validate this checkbox with toolbox validator "RequiredFieldValidator".
Upvotes: 1
Reputation: 2293
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.
Upvotes: 1