【问题标题】:Custom validator message not displaying自定义验证器消息未显示
【发布时间】:2014-08-08 06:14:04
【问题描述】:
  • VS2013、WebForms、.NET 4.51

我定义了一个 FormView,在我的 EditTemplate 中有如下内容:

 <div class="form-group">
    <asp:Label ID="lblClientClassification" CssClass="col-md-2 control-label" runat="server" for="cblClientClassifications" Text="Kind"></asp:Label>
     <div class="col-md-5">
         <asp:CheckBoxList ID="cblClientClassifications" runat="server"></asp:CheckBoxList>
         <asp:CustomValidator ID="cfvClientKinds" runat="server" Display="Dynamic" CssClass="label label-danger" ErrorMessage="XXXX" ValidationGroup="Default" OnServerValidate="cfvClientClassifications_OnServerValidate"></asp:CustomValidator>
 </div>

然后在后面的代码中:

    protected void cfvClientClassifications_OnServerValidate(object aSource, ServerValidateEventArgs aArgs)
    {
        CustomValidator cvCheckBoxKinds = aSource as CustomValidator;
        int checkedCount = 0;
        if (cvCheckBoxKinds != null)
        {
            CheckBoxList cblClientClassifications = GuiClientClassificationsFind();
            foreach (ListItem listItem in cblClientClassifications.Items)
            {
                if (listItem.Selected)
                {
                    checkedCount++;
                }
            }

            if (checkedCount == 0)
            {
                aArgs.IsValid = false;
                cvCheckBoxKinds.ErrorMessage = "Select client kind.";
            }
        }
    }

OnServerValidate 正在触发,我将验证器设置为无效以及设置错误消息(Page.IsValid 也如预期的那样为假)。但是,错误文本未显示。当我查看页面源时,我看到:

<span id="ctl00_cphMainContent_fvData_cfvClientKinds" class="label label-danger" style="display:none;">XXXX</span>

而不是我设置的错误消息以及它不可见的事实。

这里有没有人知道如何追踪这个问题?我看过类似的 SO 问题,但似乎没有一个 cmets 适用。这可能与FormView有关吗?

【问题讨论】:

  • 您在这方面取得了进展吗?
  • 不,从来没有解决这个问题。
  • 我问过你之后很快就解决了我的问题。我在下面分享我的代码作为答案。

标签: asp.net validation twitter-bootstrap webforms visual-studio-2013


【解决方案1】:

简而言之,我认为您的问题要么与您查找cblClientClassifications checkBoxList 的方式有关,要么与您上面未说明的其他代码有关。

CheckBoxList cblClientClassifications = GuiClientClassificationsFind();

我决定尝试您的案例并创建一个新的网络表单,添加了表单视图并将其绑定到北风类别表,然后在 edititemtemplate 中添加了一个复选框列表并手动填充它。添加了 CustomValidator 双击它复制了你的代码隐藏,它对我有用,除了 findcontrol 部分:GuiClientClassificationsFind(); 这是表单视图:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="CategoryID" DataSourceID="SqlDataSource1">
    <EditItemTemplate>
        ...
        <asp:CheckBoxList ID="cblClientClassifications" runat="server">
            <asp:ListItem>Bir</asp:ListItem>
            <asp:ListItem>iki</asp:ListItem>
            <asp:ListItem>Üç</asp:ListItem>
            <asp:ListItem>Dört</asp:ListItem>
        </asp:CheckBoxList>
        <asp:CustomValidator ID="cfvClientKinds" runat="server" Display="Dynamic" CssClass="label label-danger" ErrorMessage="CustomValidator" OnServerValidate="cfvClientKinds_ServerValidate"></asp:CustomValidator>
        <br />
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
        &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
</asp:FormView>

以及代码隐藏在您的代码中:

protected void cfvClientKinds_ServerValidate(object aSource, ServerValidateEventArgs aArgs)
        {
            CustomValidator cvCheckBoxKinds = aSource as CustomValidator;
            CheckBoxList cblClientClassifications = (CheckBoxList)FormView1.FindControl("cblClientClassifications");
            int checkedCount = 0;
            if (cvCheckBoxKinds != null)
            {
                foreach (ListItem listItem in cblClientClassifications.Items)
                {
                    if (listItem.Selected)
                    {
                        checkedCount++;
                    }
                }

                if (checkedCount == 0)
                {
                    aArgs.IsValid = false;
                    cvCheckBoxKinds.ErrorMessage = "Select client kind.";
                }
            }
        }

我 Ali Shahrokhi 的方法更短,效果和你的一样好..

protected void cfvClientKinds_ServerValidate(object aSource, ServerValidateEventArgs aArgs)
        {
            CustomValidator cvCheckBoxKinds = aSource as CustomValidator;
            CheckBoxList cblClientClassifications = (CheckBoxList)FormView1.FindControl("cblClientClassifications");
            aArgs.IsValid = cblClientClassifications.SelectedItem != null;
            cvCheckBoxKinds.ErrorMessage = "Select client kind.";
        }

如果您在 formview 中输入 edititemtemplate 时进行检查,您会在提交之前看到您的 customvalidators span 将在那里,就像您提到的那样,这是因为服务器尚未以某种方式将其发送给客户端。

【讨论】:

    【解决方案2】:

    先尝试不使用 CssClass="label label-danger" 引导程序的控件,然后使用下面的代码检查您的复选框:

        protected void cfvClientKinds_ServerValidate(object aSource, ServerValidateEventArgs aArgs)
        {
            aArgs.IsValid = cblClientClassifications.SelectedItem != null;
            cfvClientKinds.ErrorMessage = "Hey! this is a new message";
        }
    

    我猜你在触发上述事件之前调用了这条线:

        protected void btnValidate_Click(object sender, EventArgs e)
        {
            Page.Validate();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 2018-02-08
      • 2021-10-20
      • 1970-01-01
      相关资源
      最近更新 更多