c# - 通过 Javascript 禁用转发器内的特定验证器

标签 c# javascript jquery asp.net validation

我的转发器中有一些字段,我需要验证它们。

场景如下: 当页面加载时,我只得到一组字段(名字、姓氏等),并且得到一个链接“添加另一个用户”,如果您单击该链接,它会在底部再次添加相同的字段。

现在是代码部分: 就我而言,我需要运行中继器 4 次(因此这些字段从一开始就在页面上出现 4 次)。然后我像隐藏 <div> 一样隐藏它们。包含它们。单击按钮时,我会显示第一个隐藏的 div 等等。

一些代码(不是全部):

 <asp:Repeater ID="rptOtherPeople" runat="server">
 <HeaderTemplate>
      <table>
           <thead>
                <tr>
                     <td>
                          <h3>Други лица</h3>
                     </td>
                </tr>
           </thead>
           <tbody class="GridBody"> 
 </HeaderTemplate>
 <ItemTemplate>
      <tr class="GridRow" id="personRow" style="display: none">
           <td>
                <asp:TextBox ID="txtFirstName" CssClass="mid-inp" Text="" runat="server"></asp:TextBox>
           </td>
      </tr>
 </ItemTemplate>
 <FooterTemplate>
    </tbody>
    </table>
 </FooterTemplate>
 </asp:Repeater>

这是显示下一行的 javascript:

$(document).ready(function () {
        var peopleNum = 1;

        if ($(".GridBody tr").length > 0) {
            var tr = $(".GridBody tr")[0];
            tr.style.display = 'table-row';
            tr.setAttribute('hidden', 'false');
            var anc = tr.getElementsByTagName('a');
        }

        if ($(".GridBody tr").length > 0 && peopleNum > 0) {
            for (i = 0; i < peopleNum; i++) {
                var tr = $(".GridBody tr")[i];
                tr.style.display = 'table-row';
                tr.setAttribute('hidden', 'false');
                if (i > 0) {
                    var anc = tr.getElementsByTagName('a');
                    if (anc[i] != undefined)
                        anc[i].style.display = 'none';
                }
            }
        }
    })

    function addPerson() {
        var body = $(".GridBody");
        var indexOfNextRow = $('tr[class="GridRow"][hidden="false"]').length;
        var tr = $(".GridBody tr")[indexOfNextRow];
        tr.style.display = 'table-row';
        tr.setAttribute('hidden', 'false');
    }

问题: 例如,我希望该字段为必填字段。我放置了一个RequiredFieldValidator,并在某些情况下禁用它,并在其他情况下启用它。问题是我在页面上获得了 4 个必需字段验证器,并且我只能一次打开或关闭所有这些验证器。我只想激活其中之一。我找不到办法做到这一点,因为它们几乎相同。有什么想法吗?

我认为我无法在后面的代码中解决这个问题。我可以通过 javascript 仅使用一个RequiredFieldValidator(我如何识别我想要的)。 有些人更喜欢 jquery 验证。它是否适用于这种情况以及如何(我以前从未使用过 jquery 验证)?

编辑 1

好吧,控件并不相同。在浏览器中生成的ID为:ctl00_SPWebPartManager1_g_f6926ea5_98ba_46c1_b157_4f1ddc46885d_ctl00_Step21_otherPeople_rptOtherPeople_ctl01_rv1 ,但我无法在我的 Javascript 中访问验证器

最佳答案

您可以禁用服务器端或客户端的验证器。如果我理解您的问题,您需要的就是禁用特定的验证器,例如必填字段验证器。为此,这里有一个简单的 javascript 代码来禁用验证器。

    function DisableRFValidators() {
        var ValidatorToDisable = document.getElementById("RequiredFieldValidator2");
        ValidatorEnable(ValidatorToDisable, false);
    }

关于c# - 通过 Javascript 禁用转发器内的特定验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19947060/

相关文章:

c# - 'System.Collections.Generic.List<string>' 到 System.Collections.Generic.List<string>[*,*]

c# - 使用多个参数选择返回mysql语法错误

c# - 如何提高.NET Windows应用程序中保存多行的效率?

javascript - 将时间戳附加到 Ajax URL GET

javascript - 如何在类中创建迭代器

javascript - jquery对象实际上只是初始化构造函数 'enhanced'

c# - 动态与强类型元素

javascript - 在 Woocommerce 跟踪标签脚本中输出购买的订单项目数据

jquery - div onclick 破坏子 radio 检查行为

javascript - 更改 "inner content"的最佳方法