Customvalidator problem within Detailsview

  • Thread starter Thread starter B. Chernick
  • Start date Start date
B

B. Chernick

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>
 
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)
 
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.)
 
Back
Top