如何向动态创建的(C#)元素添加“Required”属性?

时间:2015-07-29 18:52:53

标签: c# jquery validation jquery-validate required

我想让一些TextBoxes / text输入“必需”,以便我可以使用Validate jQuery验证插件。

我试过了:

with x as 
(select customer, eff_date,
row_number() over(partition by customer order by eff_date) as rn
from tablename)
select x.customer, x.eff_date, x1.eff_date as previous_eff
from x left join x x1 on x.customer = x1.customer
and x.rn = x1.rn + 1

......但在那里不承认“必需”;所以我试过了:

boxPayeeName = new TextBox
{
    CssClass = "finaff-webform-field-input",
    ID = "payeeName",
    Required = true
};

我也尝试过这种分配属性的方式,类似的控件:

boxPayeeName = new TextBox
{
    CssClass = "finaff-webform-field-input",
    ID = "payeeName"
};
boxPayeeName.Attributes["required"] = "true";

我已通过jQuery(* .ascx)文件中的Validate jQuery插件设置验证:

boxRequesterName.Attributes.Add("required", "true");

...在设置之后......:

$(window).load(function () {
    . . .
    this.validate();
});

...但是当我运动页面时没有验证 - 当我将boxPayeeName和boxRequesterName留空时,我看不到任何验证消息。我错过了什么?

1 个答案:

答案 0 :(得分:1)

一周之前我遇到了同样的问题,这是因为"要求"属性在IE 7-9中有问题。

This is because, according to the post, all versions less than IE 10 are not entirely compliant with HTML 5.

如果您使用的是.net,我猜您是因为您正在使用C#,请尝试使用RequiredFieldValidator功能。