Welcome to Dream.In.Code
Become an Expert!

Join 150,390 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,064 people online right now. Registration is fast and FREE... Join Now!




JavaScript & CustomValidator Control

 
Reply to this topicStart new topic

JavaScript & CustomValidator Control

PsychoCoder
27 Feb, 2008 - 09:09 PM
Post #1

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
On a page I have a Repeater control, in the Repeater control I have an ItemTemplate which is CheckBox. As the Repeater is being bound it generates a CheckBox for each name in the contact list. I am trying to determine if the user selected at least 1 contact, I get the alert but the page posts back anyways. My CustomValidator looks like:


HTML
<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage="CustomValidator" Display="None" ClientValidationFunction="CheckContacts" />


My JavaScript function (CheckContacts) liiks like:


jscript

function CheckContacts(source, args)
{
var ctrl = 'repContacts';
var cbName = 'cbSelect';
var objForm = document.form1;

var errorCount = 0;

var ctrlName = ctrl + '__ctl1_' + cbName;
var chkBox = document.getElementById(ctrlName);
if(chkBox == null)
{
alert ('There was an issue checking the *'+cbName+'* check box in the *'+ctrl+'* control');
return errorCount;
}

var isChecked = false;
var i = 1; // Notice the 1, its important to get the start number correct



ctrlName = ctrl + '__ctl' + i + '_'+ cbName;
var curItem = document.getElementById(ctrlName);

if(null == curItem)
{
alert ('It was null'); // something is wrong. Probably one of the 'Customize Values'
}

while (curItem != null)
{
//to check items inside the DataGrid/Repeater
//using the != null check should keep it from getting locked up
if (curItem.checked == true)
{
isChecked = true;
}
//increment i AND get the next control
i += 1 ;
ctrlName = ctrl + '__ctl' + i + '_'+
cbName;
curItem = document.getElementById(ctrlName);
}

if (isChecked == false)
{
source.errormessage = 'Your need to select at least 1 contact in your list';
errorCount+=1;
}


if (errorCount > 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}


Alone I get no alert if no contact is selected. If I add code to the code behind that adds an onclick Event to my button like so:


csharp

btnWriteMessage.Attributes.Add("onclick", "return CheckContacts()");


I get my alert but the page still submits, which is what Im trying to stop. This is my first attempt (believe it or not) at using a CustomValidator control, so anyone got any ideas?
User is online!Profile CardPM
+Quote Post

orcasquall
RE: JavaScript & CustomValidator Control
28 Feb, 2008 - 03:42 AM
Post #2

D.I.C Head
Group Icon

Joined: 14 Sep, 2007
Posts: 158



Thanked: 3 times
Dream Kudos: 50
My Contributions
Have you tried returning a boolean from the javascript function? A "return false;" in the onclick event stops page submits. You returned a positive integer (errorCount), which javascript interpreted as true (right?), so the onclick event continued the page submit, even though the alert came up.

This post has been edited by orcasquall: 28 Feb, 2008 - 04:32 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: JavaScript & CustomValidator Control
28 Feb, 2008 - 07:46 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
orcasquall thank you! Works perfectly, I forgot the return false; in the click event of the button icon_up.gif
User is online!Profile CardPM
+Quote Post

PsychoCoder
RE: JavaScript & CustomValidator Control
28 Feb, 2008 - 09:13 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
OK one more question about this DreamInCoders, why does this script work as expected in IE6, but not in Firefox or IE7?
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:44PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month