如何让requiredfieldvalidator点击提交按钮后显示错误消息

发布于 2024-09-02 14:24:49 字数 46 浏览 2 评论 0 原文

现在,如果我移出当前文本框,则会显示错误消息。在单击提交按钮之前我不想显示它。

now, the error message will display if I move out of current textbox. I don't want to display it until I click submit button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

披肩女神 2024-09-09 14:24:49

当为验证器启用 ClientScript 时,这是不可能的。默认情况下,您的验证器会启用 ClientScript。您需要通过在源中将 EnableClientScript 设置为 False 来禁用此功能。

现在,在提交按钮的事件处理程序中调用 Page.Validate() 和 Page.IsValid 来查看每个验证器是否都通过了测试。

例子:

<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" ControlToValidate="txtFirstName" EnableClientScript="false" Display="Dynamic" SetFocusOnError="true" />

Page.Validate();
if (!Page.IsValid)
{
     //show a message or throw an exception
}

This isn't possible when ClientScript is enabled for your validators. And the ClientScript is by default enabled for your validators. You need to disable this by settings EnableClientScript to False in your source.

Now in the event handler of your submit button call Page.Validate() and Page.IsValid to see if every validators did pass the test.

Example:

<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" ControlToValidate="txtFirstName" EnableClientScript="false" Display="Dynamic" SetFocusOnError="true" />

Page.Validate();
if (!Page.IsValid)
{
     //show a message or throw an exception
}
梦亿 2024-09-09 14:24:49

在页面上的某处使用验证摘要控件...

<asp:validationsummary id="valSummary" runat="server" headertext="Validation Errors:" cssclass="ValidationSummary" />` 

然后进行验证:

<asp:textbox id="txtPostalCode" runat="server" MaxLength="250" Width="160px" text='<%# Bind("PostalCode") %>'></asp:textbox>

<asp:requiredfieldvalidator id="reqPostalCode" runat="server" errormessage="Postal code is required." controltovalidate="txtPostalCode">*</asp:requiredfieldvalidator>

如果您不需要立即反馈,请删除“*”...错误消息显示在 中> 控制提交表单的时间。

Use a validation summary control somewhere on your page...

<asp:validationsummary id="valSummary" runat="server" headertext="Validation Errors:" cssclass="ValidationSummary" />` 

Then to validate:

<asp:textbox id="txtPostalCode" runat="server" MaxLength="250" Width="160px" text='<%# Bind("PostalCode") %>'></asp:textbox>

<asp:requiredfieldvalidator id="reqPostalCode" runat="server" errormessage="Postal code is required." controltovalidate="txtPostalCode">*</asp:requiredfieldvalidator>

Remove the "*" if you don't want immediate feedback... the errormessage is displayed in the <asp:validationsummary> control when you submit the form.

(り薆情海 2024-09-09 14:24:49

通常它仅在您输入文本、再次删除然后移出文本框时出现。我认为这是设计使然。尝试更改 EnableClientScript 属性。

Normally it appears only when you enter text, delete it again and then move out of the textbox. I think this is by design. Try to change EnableClientScript property.

雾里花 2024-09-09 14:24:49

将验证器的 forecolor 属性设置为页面的背景颜色。然后在提交按钮的 onclientclick 中,将 css color 属性更改为所需的颜色:

<asp:CompareValidator ID="birthdaycheck" runat="server" ErrorMessage="" 
    Text="*Required" ControlToValidate="birthday" ValidationGroup="rfi" 
    Operator="NotEqual"  ForeColor="#F3F3E9"  />  


<asp:Button ID="btnFinish" runat="server" Text="Finish"
    CausesValidation="true" CommandName="MoveComplete" CssClass="navButton" 
    ValidationGroup="rfi" 
    OnClientClick="$('#wizard_birthdaycheck').css('color','red');" />

Set the forecolor property of the validator to the background color of your page. Then in the onclientclick of the submit button, change the css color property to the desired color:

<asp:CompareValidator ID="birthdaycheck" runat="server" ErrorMessage="" 
    Text="*Required" ControlToValidate="birthday" ValidationGroup="rfi" 
    Operator="NotEqual"  ForeColor="#F3F3E9"  />  


<asp:Button ID="btnFinish" runat="server" Text="Finish"
    CausesValidation="true" CommandName="MoveComplete" CssClass="navButton" 
    ValidationGroup="rfi" 
    OnClientClick="$('#wizard_birthdaycheck').css('color','red');" />
白云悠悠 2024-09-09 14:24:49

您可以为不希望进行验证的按钮设置 CausesValidation="False"

<asp:Button ID="btnCancel" runat="server" Text="cancel" CausesValidation="False"
                            onclick="btnCancel_Click"/>

You could set CausesValidation="False" for the button for which you don't want validation to happen.

<asp:Button ID="btnCancel" runat="server" Text="cancel" CausesValidation="False"
                            onclick="btnCancel_Click"/>
墨洒年华 2024-09-09 14:24:49

尝试这个来创建动态单选按钮以及所需的字段验证器......

    TableRow trow4 = new TableRow();
    trow4.Style.Add("width", "100px");
    TableCell tcel4 = new TableCell();
    Label lb4 = new Label();
    lb4.Text = Resources.QcLabelName.Gender;
    tcel4.Controls.Add(lb4);
    CSSCell(tcel4);
    table.Rows.Add(trow4);
    RadioButtonList rblist = new RadioButtonList();
    rblist.ID = "rbtnmalendfemale";
    rblist.Items.Add("Male");
    rblist.Items.Add("Female");
    tcel4.Controls.Add(rblist);
    trow4.Cells.Add(tcel4);
    table.Rows.Add(trow4);
    rblist.Visible = true;
    RequiredFieldValidator rFV5 = new RequiredFieldValidator();
    TableCell tcl46 = new TableCell();
    rFV5.ControlToValidate = "rbtnmalendfemale";
    rFV5.ErrorMessage = "Gendor Selection Is Mandatory";
    rFV5.Style.Add("color", "Red");
    rFV5.ID = "Reqfield9";
    tcl46.Controls.Add(rFV5);
    trow4.Cells.Add(tcl46);
    table.Rows.Add(trow4);
    rFV5.Visible = true;

Try this one for creating dynamic radio buttons along with required field validators...

    TableRow trow4 = new TableRow();
    trow4.Style.Add("width", "100px");
    TableCell tcel4 = new TableCell();
    Label lb4 = new Label();
    lb4.Text = Resources.QcLabelName.Gender;
    tcel4.Controls.Add(lb4);
    CSSCell(tcel4);
    table.Rows.Add(trow4);
    RadioButtonList rblist = new RadioButtonList();
    rblist.ID = "rbtnmalendfemale";
    rblist.Items.Add("Male");
    rblist.Items.Add("Female");
    tcel4.Controls.Add(rblist);
    trow4.Cells.Add(tcel4);
    table.Rows.Add(trow4);
    rblist.Visible = true;
    RequiredFieldValidator rFV5 = new RequiredFieldValidator();
    TableCell tcl46 = new TableCell();
    rFV5.ControlToValidate = "rbtnmalendfemale";
    rFV5.ErrorMessage = "Gendor Selection Is Mandatory";
    rFV5.Style.Add("color", "Red");
    rFV5.ID = "Reqfield9";
    tcl46.Controls.Add(rFV5);
    trow4.Cells.Add(tcl46);
    table.Rows.Add(trow4);
    rFV5.Visible = true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文