动态创建的requiredfieldvalidator绕过客户端验证

发布于 2024-12-22 06:13:09 字数 1211 浏览 2 评论 0原文

我正在尝试在运行时向我的页面添加必填字段验证器。 那里没什么奇特的:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:ValidationSummary ID="ValidationSummary1"
        runat="server" ShowMessageBox="True" ShowSummary="False" />
</form>

和@ codebehind

 protected void Page_Init(object sender, EventArgs e)
    {
        RequiredFieldValidator theValid = new RequiredFieldValidator();
        theValid.ID = "0000" + "RFV";
        theValid.ControlToValidate = TextBox1.ID;
        theValid.ErrorMessage = "Message here"
        theValid.Text = "*";
        theValid.Display = ValidatorDisplay.Dynamic;
        theValid.EnableClientScript = true;
        theValid.EnableViewState = true;
        theValid.SetFocusOnError = true;
        theValid.Enabled = true;
        theValid.Visible = true;
        Page.Validators.Add(theValid);
        form1.Controls.Add(theValid);
    }

当我单击按钮时,它只验证服务器端,而不验证客户端。我缺少什么?

TIA。

I am trying to add a required field validator to my page at runtime.
Nothing fancy there:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:ValidationSummary ID="ValidationSummary1"
        runat="server" ShowMessageBox="True" ShowSummary="False" />
</form>

and @ codebehind

 protected void Page_Init(object sender, EventArgs e)
    {
        RequiredFieldValidator theValid = new RequiredFieldValidator();
        theValid.ID = "0000" + "RFV";
        theValid.ControlToValidate = TextBox1.ID;
        theValid.ErrorMessage = "Message here"
        theValid.Text = "*";
        theValid.Display = ValidatorDisplay.Dynamic;
        theValid.EnableClientScript = true;
        theValid.EnableViewState = true;
        theValid.SetFocusOnError = true;
        theValid.Enabled = true;
        theValid.Visible = true;
        Page.Validators.Add(theValid);
        form1.Controls.Add(theValid);
    }

When I click the button it only validates server-side, but not client-side. What am I missing?

TIA.

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

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

发布评论

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

评论(2

多孤肩上扛 2024-12-29 06:13:09

您可以使用 jquery 检查您的代码。

function TextBoxValidate() {
$('input:[type="text"]:[id*="TextBox1"]').each(function () { var txtId = this.id; $('span').each(function () { if (this.controltovalidate == txtId) ValidatorValidate(this); }); }); 
}
function InitValidationOnClick()
{  
$('input:[id*="Button1"]').each(function(){$(this).on('click', function(){TextBoxValidate(); return Page_IsValid;});});
// or other case
//$('form:[id*="form1"]').each(function(){$(this).on('submit', function(){TextBoxValidate(); return Page_IsValid;});});
}
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(InitValidationOnClick);

如果页面中存在验证器,它将在客户端的单击(提交)事件上进行验证。

You can to check your code with jquery.

function TextBoxValidate() {
$('input:[type="text"]:[id*="TextBox1"]').each(function () { var txtId = this.id; $('span').each(function () { if (this.controltovalidate == txtId) ValidatorValidate(this); }); }); 
}
function InitValidationOnClick()
{  
$('input:[id*="Button1"]').each(function(){$(this).on('click', function(){TextBoxValidate(); return Page_IsValid;});});
// or other case
//$('form:[id*="form1"]').each(function(){$(this).on('submit', function(){TextBoxValidate(); return Page_IsValid;});});
}
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(InitValidationOnClick);

If validator exists in page it will validate on click (submit) event client-side.

不再见 2024-12-29 06:13:09

大家好,尝试一下单选按钮和必需字段验证器

代码

    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;******

Hello All Try This one For Radio Buttons Along With RequiredField Validator

Code

    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 和您的相关数据。
原文