I have a customvalidator within an EditItemTemplate, in order to validate a
dropdown list. This is the first time I've tried to implement a cv in
Asp.Net 2.0, let alone a DetailsView. On this page are also several required
field validators for unrelated textboxes. The problem, in summary, is that
when I blank one of those textbox fields and hit Update, the textbox
validator fires as expected but so does the ddl validator for no apparent
reason.
Even more confusing is that that, although I see no other errors, the
windows.alert I put in the javascript funtion is not working. Here is the
html: (The idea is to fire the error if the ddl selected value is -1. Also,
the ddlUnit handle is initialized in VB code during the detailsview DataBound
event.)
<EditItemTemplate>
<asp:DropDownList ID="ddlUnit" runat="server"
DataSourceID="ObjectDataSourceUnit"
DataTextField="Item" DataValueField="PK">
</asp:DropDownList>
<asp:CustomValidator ID="cvUnit" runat="server"
ClientValidationFunction="ValidateUnit"
ControlToValidate="ddlUnit" ErrorMessage="Select a
Valid Unit">*</asp:CustomValidator>
<asp:ObjectDataSource ID="ObjectDataSourceUnit"
runat="server"
OldValuesParameterFormatString="{0}"
SelectMethod="GetData"
TypeName="GM_Project_Application_2.GMPATableAdapters.ztblPicklistTableAdapter">
<SelectParameters>
<asp:Parameter DefaultValue="ABACUS"
Name="Group" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<script language="javascript" type="text/javascript">
function ValidateUnit(sender, args)
{
var r = <%= ddlUnit.ClientID %>;
var currentValue = r.GetValue();
window.alert(currentValue);
if (currentValue >= 0)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
</script>
</EditItemTemplate>
bruce barker (sqlwork.com) - 04 Dec 2007 16:36 GMT
Unless you've added the code, a ddl does not have a GetValue() method, nor do
you need one, as the value is passed. also to do a numeric compare, you need
a cast
try:
function ValidateUnit(sender, args)
{
args.IsValid = (new Number(args.Value)) >= 0;
return args.IsValid;
}
-- bruce (sqlwork.com)
> I have a customvalidator within an EditItemTemplate, in order to validate a
> dropdown list. This is the first time I've tried to implement a cv in
[quoted text clipped - 49 lines]
> </script>
> </EditItemTemplate>
B. Chernick - 05 Dec 2007 20:51 GMT
Thanks! Worked perfectly.
(Even better, I can use one function to validate all my dropdowns. That
excessively complex javascript function was modeled after ones in my previous
job where we were using nothing but guid values and 3rd party dropdowns.)
> Unless you've added the code, a ddl does not have a GetValue() method, nor do
> you need one, as the value is passed. also to do a numeric compare, you need
[quoted text clipped - 64 lines]
> > </script>
> > </EditItemTemplate>