Is it possible for you to put a break point in the javascript and follow it to see where its failing? Perhaps something is being returned as undefined.
Okay, I went home last night and worked and worked on this thing to no avail. Finally, I have decided to just use my own themed buttons native to .NET to eliminate this whole Postback problem I have been dealing with.
However, after changing out my Obout buttons with asp:imagebutton and asp:textbox, your javascript is working as expected with my checkboxes in my GridView. But, I guess since the asp:button performs an OnClientClick function, my asp:validation control
is not firing for my asp:textbox.
I tested my asp:validation control and it works perfectly if I remove the OnClientClick from my button.
Any suggestions?
Here is the javascript I am using:
<script type="text/javascript">
function validate() {
var counter = 0;
var gridView = document.getElementById('<%# UploadedFilesList.ClientID %>');
for (var i = 1; i < gridView.rows.length; i++) {
var cell = gridView.rows[i].cells[0];
var inputs = cell.getElementsByTagName('input');
for (var j = 0; j < inputs.length; j++) {
if (inputs[j].type == "checkbox") {
if (inputs[j].checked) {
counter++;
}
}
}
}
if (counter > 0) {
return true;
}
else {
alert("You must select at least one file before you can continue");
return false;
}
}
</script>
Lets just use a Custom Validator and remove the OnClientClick line. Then you can let your RequiredField validator and this custom validator work as expected.
// Javascript
<script type="text/javascript">
function validate(sender, args) {
var counter = 0;
var gridView = document.getElementById('<%# MyGridView.ClientID %>');
for (var i = 1; i < gridView.rows.length; i++) {
var cell = gridView.rows[i].cells[0];
var inputs = cell.getElementsByTagName('input');
for (var j = 0; j < inputs.length; j++) {
if (inputs[j].type == "checkbox") {
if (inputs[j].checked) {
counter++;
}
}
}
}
if (counter > 0) {
args.IsValid = true;
}
else {
alert("You must select at least one file before you can continue");
args.IsValid = false;
}
}
</script>
// modified button, has no onclientclick
<asp:Button ID="DownloadZipFileBtn" runat="server" Text="Download Zip File"
onclick="DownloadZipFileBtn_Click" />
// added custom validator
<asp:CustomValidator ID="myCustomValidator" runat="server"
ClientValidationFunction="validate" />
N_EvilScott
Star
8303 Points
1488 Posts
Re: TextBox Validation after postback
Feb 13, 2012 08:21 PM|LINK
Is it possible for you to put a break point in the javascript and follow it to see where its failing? Perhaps something is being returned as undefined.
mattcase
Member
380 Points
538 Posts
Re: TextBox Validation after postback
Feb 14, 2012 04:23 PM|LINK
Okay, I went home last night and worked and worked on this thing to no avail. Finally, I have decided to just use my own themed buttons native to .NET to eliminate this whole Postback problem I have been dealing with.
However, after changing out my Obout buttons with asp:imagebutton and asp:textbox, your javascript is working as expected with my checkboxes in my GridView. But, I guess since the asp:button performs an OnClientClick function, my asp:validation control is not firing for my asp:textbox.
I tested my asp:validation control and it works perfectly if I remove the OnClientClick from my button.
Any suggestions?
Here is the javascript I am using:
<script type="text/javascript"> function validate() { var counter = 0; var gridView = document.getElementById('<%# UploadedFilesList.ClientID %>'); for (var i = 1; i < gridView.rows.length; i++) { var cell = gridView.rows[i].cells[0]; var inputs = cell.getElementsByTagName('input'); for (var j = 0; j < inputs.length; j++) { if (inputs[j].type == "checkbox") { if (inputs[j].checked) { counter++; } } } } if (counter > 0) { return true; } else { alert("You must select at least one file before you can continue"); return false; } } </script>N_EvilScott
Star
8303 Points
1488 Posts
Re: TextBox Validation after postback
Feb 14, 2012 04:46 PM|LINK
Lets just use a Custom Validator and remove the OnClientClick line. Then you can let your RequiredField validator and this custom validator work as expected.
// Javascript <script type="text/javascript"> function validate(sender, args) { var counter = 0; var gridView = document.getElementById('<%# MyGridView.ClientID %>'); for (var i = 1; i < gridView.rows.length; i++) { var cell = gridView.rows[i].cells[0]; var inputs = cell.getElementsByTagName('input'); for (var j = 0; j < inputs.length; j++) { if (inputs[j].type == "checkbox") { if (inputs[j].checked) { counter++; } } } } if (counter > 0) { args.IsValid = true; } else { alert("You must select at least one file before you can continue"); args.IsValid = false; } } </script> // modified button, has no onclientclick <asp:Button ID="DownloadZipFileBtn" runat="server" Text="Download Zip File" onclick="DownloadZipFileBtn_Click" /> // added custom validator <asp:CustomValidator ID="myCustomValidator" runat="server" ClientValidationFunction="validate" />siamand
Member
416 Points
307 Posts
Re: TextBox Validation after postback
Feb 14, 2012 05:46 PM|LINK
hi if you use
instead of
i think your problem will be solved ..
Regards
mattcase
Member
380 Points
538 Posts
Re: TextBox Validation after postback
Feb 14, 2012 06:37 PM|LINK
You are without a doubt the most helpful person I have met on this forum.
I appreciate it!
Thanks.