UserControl CustomValidator调用外部Javascript

时间:2018-05-04 14:00:22

标签: javascript asp.net customvalidator

我有一个带有Datagrid的用户控件。当用户保存其修改时,我需要通过调用我的Javascript文件来检查一些文本框。 (它将触发我的验证器,如果弹出窗口显示错误)

我的ASPX:

<script type="text/javascript" src='/scripts/production_cost.js'></script>

<ContentTemplate>
  <asp:DataGrid ID="ProdCostGrid" runat="server" AutoGenerateColumns="False" BorderColor="#f0f0f0" BorderStyle="None" BorderWidth="0px" CellPadding="0">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<Columns>
   <asp:BoundColumn Visible="False" DataField="Id" HeaderText="Id"></asp:BoundColumn>
     <asp:TemplateColumn>
       <ItemTemplate>
       Amount:&nbsp;<asp:TextBox ID="ProductionCostLineField" Text='<%# ToText(Eval("ProductionCostEuro")) %>'
                                                TabIndex="24" runat="server" Width="80px" MaxLength="13"></asp:TextBox>&nbsp;EUR
                                        </ItemTemplate>
                                        <ItemStyle Width="170px" HorizontalAlign="Right"></ItemStyle>
                                    </asp:TemplateColumn>
                              </Columns>
                            </asp:DataGrid>
//here I am suppose to put my Validator
<asp:CustomValidator ID="AmountCustomValidator" runat="server" Display="None" ErrorMessage="Amount must be filled" ClientValidationFunction="ValidateAmount" />
                        </ContentTemplate>

我在其他文件夹中的Javascript:

function ValidateAmount(source, args) {
args.IsValid = true;
$('input[id*="ProductionCostLineField"]').blur(function () {
    var amount = this.value;
    $('input[id*="ProductionCostInvoiceToLineField"]').each(function () {
        var textInvoicedBy = this.value;
        if (amount == '' || amount == '0') {

        } else {
            if (this.value != '' || amount == '' || amount == '0') {
                args.IsValid = true;
            }
            if ((amount != '' || amount != '0') && textInvoicedBy == '') {
                alert("You must inform the field 'Invoiced By'");
                args.IsValid = false;
            }
        }
    });
});
}

0 个答案:

没有答案