从属性设置RequiredFieldValidator上的ValidationGroup未正确设置

时间:2017-10-06 10:43:53

标签: c# asp.net properties requiredfieldvalidator

我正在尝试从我页面上的属性动态设置我的必需字段验证器ValidationGroup,但是它们没有触发。如果我手动设置字符串,它会触发。我的假设是它没有正确地将属性拉入ValidationGroup。我错过了什么吗?

<asp:RequiredFieldValidator runat="server" ID="rfvHouseName" ControlToValidate="txtHouseName" ErrorMessage="Please enter a house name/no." ForeColor="Red" ValidationGroup="<%#ValidationGroup%>"><i class="fa fa-star requiredFieldStar"></i></asp:RequiredFieldValidator>
<asp:TextBox ID="txtHouseName" runat="server" MaxLength="50" CssClass="form-control" />

private static string _validationGroup = "NewAddress";

public virtual string ValidationGroup
{
    get { return _validationGroup; }
    set { _validationGroup = value; }        
}

1 个答案:

答案 0 :(得分:2)

您必须在

后面的代码中设置它
rfvHouseName.ValidationGroup = ValidationGroup;

或者,如果你真的想在线使用它,你必须像这样使用它

<asp:RequiredFieldValidator ValidationGroup='<%# ValidationGroup %>'

然而,对于第二个工作,您必须每次从代码后面调用DataBind()

protected void Page_Load(object sender, EventArgs e)
{
    DataBind();
}