Wrox Programmer Forums
|
BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9
This is the forum to discuss the Wrox book Professional ASP.NET 3.5: In C# and VB by Bill Evjen, Scott Hanselman, Devin Rader; ISBN: 9780470187579
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2009, 08:30 PM
Authorized User
 
Join Date: Dec 2008
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
Default Trouble with ARGS in Custom Validator

Somehow ARG seems an apt term to describe this.....

Based on pages 302-304, I built 2 custom validators on my Profile page. One of them works perfectly, so I did the 2nd one the same way (or so I think).

The second custom validator throws an error, saying ARGS is undefined, when clearly my Javascript function does specify (source, args) as parameters. Refreshing VWD, rebuilding the page, and closing/re-opening the browser do not help.

Here are the pertinent code snippets:

Profile.aspx (the client JS)

Code:
<script type="text/javascript">

function ValidateStates(source, args) {
var country = document.getElementById('<%=ddlCountry.ClientID %>');
var isUSCAMX = true;
switch (country.value) {
case 'US':
var state = document.getElementById('<%=ddlUnitedStates.ClientID %>');
break;
case 'CA':
var state = document.getElementById('<%=ddlCanada.ClientID %>');
break;
case 'MX':
var state = document.getElementById('<%=ddlMexico.ClientID %>');
break;
default:
isUSCAMX =
false;
break;
}
if (isUSCAMX == true && state.value == '999') {
args.IsValid =
false;
}
else {
args.IsValid =
true;
}
}
</script>
Profile.aspx (the pertinent controls)
Code:
<tr>
<td class="centerfield">
<asp:DropDownList ID="ddlUnitedStates" runat="server" DataSourceID="SqlDataSource1" DataTextField="sName" DataValueField="sAbbr" ToolTip="Required if USA is selected as Country. If Canada or Mexico is selected as Country, this drop-down list shows their states or provinces." CssClass="visible">
<asp:ListItem Selected="True">&lt;select&gt;</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnetdbConnectionString %>" SelectCommand="SELECT sName, sAbbr FROM aspnet_States WHERE (sCountry = N'US') ORDER BY sName"></asp:SqlDataSource>
<asp:DropDownList ID="ddlCanada" runat="server" DataSourceID="SqlDataSource2" DataTextField="sName" DataValueField="sAbbr" ToolTip="If USA or Mexico is selected as Country, this drop-down list shows their states. Required if USA is selected as Country." CssClass="hidden">
<asp:ListItem Selected="True">&lt;select&gt;</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:aspnetdbConnectionString %>" SelectCommand="SELECT sName, sAbbr FROM aspnet_States WHERE (sCountry = N'CA') ORDER BY sName"></asp:SqlDataSource>
<asp:DropDownList ID="ddlMexico" runat="server" DataSourceID="SqlDataSource3" DataTextField="sName" DataValueField="sAbbr" ToolTip="If USA or Canada is selected as Country, this drop-down list shows their states or provinces. Required if USA is selected as Country." CssClass="hidden">
<asp:ListItem Selected="True">&lt;select&gt;</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:aspnetdbConnectionString %>" SelectCommand="SELECT sName, sAbbr FROM aspnet_States WHERE (sCountry = N'MX') ORDER BY sName"></asp:SqlDataSource>
</td>
<td>
<asp:CustomValidator ID="CustomValidator2" runat="server" ErrorMessage="required if country is United States, Canada, or Mexico" CssClass="boldred" ControlToValidate="ddlCountry" ClientValidationFunction="ValidateStates" onservervalidate="CustomValidator2_ServerValidate" Display="Dynamic" ForeColor=""></asp:CustomValidator>
</td>
</tr>
The pertinent code behind / server_validate

Code:
protectedvoid ddlCountry_PreRender(object sender, EventArgs e)
 
 

{
string country = (string)ddlCountry.Text.ToUpper();
/* ignore the next line, it's an unrelated feature that works fine */
/* ValidateStates() is the one with the problem */
ddlCountry.Attributes.Add(
"onchange", "swapStates();");
ddlCountry.Attributes.Add("onblur", "ValidateStates();");
} protectedvoid CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args) {
string state = "";
Boolean isUSCAMX = true;
switch (ddlCountry.SelectedValue.ToString()) {
case"US":
state = ddlUnitedStates.SelectedValue.ToString();
break;
case"CA":
state = ddlCanada.SelectedValue.ToString();
break;
case"MX":
state = ddlMexico.SelectedValue.ToString();
break;
default:
isUSCAMX =
false;
break;
}
if (isUSCAMX == true && state == "999")
{
args.IsValid =
false;
/* these arg statements throw the following error message: */
/* 'undefined' is null or not an object */
/* I don't know why, it's declared at the very top of this function */
}
else
{
args.IsValid =
true;
}
}
Any assistance would be most appreciated. I realize it's a lot to digest.

Thanks, Eddie
__________________
~ Eddie McHam

Last edited by eddiemcham; June 24th, 2009 at 09:39 AM..
 
Old June 24th, 2009, 11:41 AM
Authorized User
 
Join Date: Dec 2008
Posts: 26
Thanks: 3
Thanked 0 Times in 0 Posts
Default Never mind. Suddenly, it's working now.

I wiped out all the related code and started over from scratch, again following pp 302-4. Now it's all working correctly.

I cannot for the life of me figure out why all of a sudden 'args' is no longer undefined and the custom validator works now. I'm sure I did the same thing before, but who knows.

Anyway, the bloomin' thing is fixed. I'm taking a Mountain Dew break.

Thanks anyway!!!!
~e
__________________
~ Eddie McHam





Similar Threads
Thread Thread Starter Forum Replies Last Post
DDL Custom Validator AspNetGuy BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 June 6th, 2008 05:17 PM
Custom Validator against a database Steve Spicer ASP.NET 2.0 Professional 16 January 12th, 2008 06:25 AM
custom validator question kscdave Classic ASP Professional 3 April 24th, 2005 07:11 AM
Custom Validator bmains ASP.NET 1.0 and 1.1 Basics 4 December 18th, 2003 11:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.