CustomValidator in user control (.ascx)? Options
tkahn
Posted: Wednesday, September 26, 2007 3:12:08 PM

Rank: Fanatic

Joined: 11/24/2006
Posts: 323
Location: Stockholm, Sweden
I have a user control in which I have a form. In this form there is a checkbox. This checkbox has to be checked by the user before he/she can submit the form properly. In order to validate a checkbox using ASP.NET you have to use a custom validator. I haven't used custom validators a lot, but I read some examples on the net and it seems like you have a client side validation function (javascript) and a server side validation (c#). My functions look like this:

Client side:
Code:

<script type="text/javascript" language="javascript">
function ClientValidateAvtalGodkant(sender, args)
{
    args.IsValid = document.all["cbxAvtalGodkant"].checked;
}
</script>


Server side:
Code:

protected void ServerValidateAvtalGodkant(object source, ServerValidateEventArgs args)
{
args.IsValid = cbxAvtalGodkant.Checked;
}


The ID of the checkbox in my form is cbxAvtalGodkant. The result when I run this code is that the validation is false whether I've checked the checkbox or not. And the reason seems to be that when the code is placed inside a user control, all ID's are replaced. The ID of my checkbox goes from cbxAvtalGodkant to Intresseanmalan_7_cbxAvtalGodkant and the javascript can't get a reference to the object and therefore not see if it's checked or not.

I just want know if anyone has found a nice workaround?

Web Developer at Kärnhuset - http://www.karnhuset.net - Stockholm, Sweden
tkahn
Posted: Wednesday, September 26, 2007 3:30:14 PM

Rank: Fanatic

Joined: 11/24/2006
Posts: 323
Location: Stockholm, Sweden
Found a solution myself and I'm posting it here in case anyone else should stumble into the same problem. The key is to replace the hard coded ID of the element that needs to be referenced in the javascript. This is done using a small snippet of server-side code, like this:

Code:

<script type="text/javascript" language="javascript">
function ClientValidateAvtalGodkant(sender, args)
{
args.IsValid = document.all["<%= cbxAvtalGodkant.ClientID  %>"].checked;
}
</script>


Keep on coding!

Web Developer at Kärnhuset - http://www.karnhuset.net - Stockholm, Sweden
acullen
Posted: Wednesday, September 26, 2007 3:44:59 PM

Rank: Devotee

Joined: 4/13/2007
Posts: 57
Location: Arlington, VA
Not sure if this will help or not, but I had a case where I needed an "OnClientClick" to mainpulate the DOM, and I just passed the keyword "this" as the parameter "element". For some reason I can't fathom, I needed to then do a "document.getElementById(element.id)" to work with the DOM node.

Your other choice would be to generate the hook into your javascript program in the codebehind, using the "ClientId" property.
acullen
Posted: Wednesday, September 26, 2007 3:46:20 PM

Rank: Devotee

Joined: 4/13/2007
Posts: 57
Location: Arlington, VA
Wow - guess I should've checked before uploading that comment I started 30 minutes ago, huh? :blush:
tkahn
Posted: Wednesday, September 26, 2007 4:08:41 PM

Rank: Fanatic

Joined: 11/24/2006
Posts: 323
Location: Stockholm, Sweden
Update: you need to do a check for how you reference the element. The last snippet I posted will generate an error in FireFox. So use this as an example instead:

Code:

<script type="text/javascript" language="javascript">
function ClientValidateAvtalGodkant(sender, args)
{
    args.IsValid = document.all ? document.all["<%= cbxAvtalGodkant.ClientID  %>"].checked : document.getElementById("<%= cbxAvtalGodkant.ClientID  %>").checked;
}
</script>


Web Developer at Kärnhuset - http://www.karnhuset.net - Stockholm, Sweden
tkahn
Posted: Wednesday, September 26, 2007 4:14:50 PM

Rank: Fanatic

Joined: 11/24/2006
Posts: 323
Location: Stockholm, Sweden
No worries! I'm very happy that you replied and that you came to the same conclusion as I. :)
Thanks!

Using "this" is often a very elegant solution since it's more object oriented, but I think I'll just let my code stay as it is for now and carry on with all the other problems that have to be solved before I can get my site up and running.

Web Developer at Kärnhuset - http://www.karnhuset.net - Stockholm, Sweden
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.