Enabling RequiredFieldValidator From Code Behind
我正在从后面的代码中显示 requiredfieldvalidator 的错误消息,但显示设置为无或隐藏可见。我试图做 .visible = true 但它仍然是隐藏的。我怎样才能让它显示错误消息?文本框位于 GridView.
内
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <ItemTemplate> </asp:TextBox> </ItemTemplate> </asp:TemplateField> TextBox txtSSN = (TextBox)r.FindControl("txtSSN"); if (txtSSN.Text =="") { RequiredFieldValidator RequiredFieldValidator1 = (RequiredFieldValidator)r.FindControl("RequiredFieldValidator1"); RequiredFieldValidator1.Visible = true; RequiredFieldValidator1.Enabled = true; RequiredFieldValidator1.ErrorMessage ="This field is required."; } |
将
1 | RequiredFieldValidator1.IsValid = false; |
您不必担心可见性,这会自动发生。
验证器在验证无效时显示自己,因此通过
触发它
1 | RequiredFieldValidator1.Validate(); |
Performs validation on the associated input control and updates the IsValid property.
该字段不能在网格中才能使用
如果它在网格中,请使用