用户控件内部的下拉控件的RequiredFieldValidator不会触发

本文关键字:控件 内部 用户 RequiredFieldValidator | 更新日期: 2023-09-27 18:02:16

下面是我的代码的测试场景。1)一旦用户选择了网页上的单选按钮之一。Aspx中,会出现一个模态弹出式扩展器。2)用户控件(SSL_Ticket.ascx)在模态弹出窗口内定义。3)为包含在用户控件中的下拉列表定义RequiredFieldValidator。4)如果用户从下拉列表中选择"0"值,则不显示验证错误提示。

<标题> 代码

Webpage.aspx

                     <asp:RadioButtonList ID="RadioButtonListForTicket" runat="server"  AutoPostBack="true"
                         OnSelectedIndexChanged="radioButtonListForTicket_OnSelectedIndexChanged">
                            <asp:ListItem Selected="True">No</asp:ListItem>
                            <asp:ListItem>Yes</asp:ListItem>
                      </asp:RadioButtonList>
                      <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtenderForTicket" runat="server" BackgroundCssClass="popUpStyle"
                          DropShadow="true" PopupControlID="divTicketPreview" PopupDragHandleControlID="panelDragHandle"
                          TargetControlID="btnForPopupAppear"   CancelControlID="btnForPopupDisappear"/>


Webpage.aspx.cs

protected void radioButtonListForTicket_OnSelectedIndexChanged(object sender, System.EventArgs e)
{
    if (RadioButtonListForTicket.SelectedItem.Text.ToString().Equals("Yes"))
    {
        // Check if the sites are selected
        updateSelectionCount();
        updateListOfSites();
        if (selectionCount == 0)
        {
            lblSSLTicketSelection.Text = "Please select a site.";
            RadioButtonListForTicket.SelectedValue = "No";
            return;
        }
        else
        {
            lblSSLTicketSelection.Text = "";
        }
        ....
        ModalPopupExtenderForTicket.Show();
    }
}

SSL_Ticket.ascx

           <asp:DropDownList ID="cmbRootCause" runat="server" Width="255px" OnSelectedIndexChanged="cmbRootCause_SelectedIndexChanged" AutoPostBack="true"
                CausesValidation="true">
                <asp:ListItem Value="0">Select</asp:ListItem>
                <asp:ListItem Value="1">Item1</asp:ListItem>
           </asp:DropDownList>
           <asp:RequiredFieldValidator ID="reqdFieldForRootCause" runat="server" ControlToValidate="cmbRootCause" InitialValue="Select" 
               ErrorMessage="Please select root cause" ValidationGroup="validateRootCause"  Visible="false" Display="Dynamic" EnableClientScript="true">
           </asp:RequiredFieldValidator>

SSL_Ticket.ascx.cs

    protected void cmbRootCause_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cmbRootCause.SelectedItem.ToString().Equals("Other"))
        {
            lblcmbRootCause.Text = "";
            lblcmbRootCause.Visible = false;
            txtRootCauseOther.Visible = true;
        }
        else if (cmbRootCause.SelectedItem.ToString().Equals("Select"))
        {
            lblcmbRootCause.Visible = true;
            lblcmbRootCause.Text = "Please select root cause";
        }
        else
        {
            lblcmbRootCause.Text = "";
            lblcmbRootCause.Visible = false;
            txtRootCauseOther.Visible = false;
        }
    }

我确实浏览了几个解决方案(ValidateProperty,客户端验证,RangeValidation等),但它没有触发验证文本。

这个没有帮助-在用户控件中处理RequiredFieldValidator

我非常感谢你的帮助。

谢谢! !

用户控件内部的下拉控件的RequiredFieldValidator不会触发

从必填字段验证器中删除visible = false属性,默认情况下它们不会在开始显示。

<asp:RequiredFieldValidator ID="reqdFieldForRootCause" runat="server" ControlToValidate="cmbRootCause" InitialValue="Select" 
               ErrorMessage="Please select root cause" ValidationGroup="validateRootCause"  **Visible="false"** Display="Dynamic" EnableClientScript="true">
           </asp:RequiredFieldValidator>

在你的'RequiredFieldValidator'为你的DropDownList,你需要删除这个:

InitialValue="Select"