首页 > 解决方案 > UserControl CustomValidator 调用外部 Javascript

问题描述

我有一个带有 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;
            }
        }
    });
});
}

标签: javascriptasp.netcustomvalidator

解决方案


推荐阅读