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"><select></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"><select></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"><select></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