RequiredFieldValidator无法以编程方式工作

时间:2011-02-21 10:26:05

标签: c# .net asp.net requiredfieldvalidator

正如标题所说。可能有什么不妥?

我的网页上有一个常规的TextBox - <asp:TextBox ID="tb" runat="server" />

以下作品:

<asp:RequiredFieldValidator ID="r" runat="server" ControlToValidate="tb" ErrorMessage="error" />

以下不起作用:

RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ID = "rfv";
rfv.ControlToValidate = tb.ID;
rfv.ErrorMessage = "error";

1 个答案:

答案 0 :(得分:3)

您需要添加必填字段验证器

RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ID = "rfv";
rfv.ControlToValidate = tb.ID;
rfv.ErrorMessage = "error";
yourFormId.Controls.Add(rfv);// you need to add this