这里是我采取在C#中的服务器端解决方案,以模仿上面的回答,有兴趣的人士:
<asp:TextBox ID="txtStudentComments" runat="server"
Rows="8" Width="100%"
ToolbarCanCollapse="False" ValidationGroup="vg1" />
<asp:CustomValidator ID="cv1" runat="server" ControlToValidate="txtStudentComments"
ErrorMessage="THESE COMMENTS DO NOT SEEM RIGHT. PLEASE REVIEW THEM AGAIN!" SetFocusOnError="true"
Font-Bold="True" Font-Size="Medium" ValidationGroup="vg1" OnServerValidate="cv1_ServerValidate"></asp:CustomValidator>
和服务器上:
//validate of the comment contains some specific words which imply the TET has not reviewed the comments!
protected void cv1_ServerValidate(object source, ServerValidateEventArgs args)
{
CustomValidator cv = (CustomValidator)source;
GridViewRow gvRow = (GridViewRow)cv.NamingContainer;
TextBox editor = (TextBox)gvRow.FindControl("txtStudentComments");
if (editor.Text.ToUpper().Contains("FACILITATOR TO INSERT COMMENTS HERE PLEASE"))
args.IsValid = false;
else
args.IsValid = true;
}
这两条线是关键的。
CustomValidator cv = (CustomValidator)source;
GridViewRow gvRow = (GridViewRow)cv.NamingContainer;
的NamingContainer会在我的情况下GridViewRow,但它可能是依赖于你的程序的整个页面。无论哪种方式,它允许我找到我想要的控件,相对于ControlToValidate对象,如上所述将返回ClientID。
嗨穆萨。我试图做到这一点,但它返回'undefined' – DJPB 2010-09-03 14:44:12
这真的很有趣,因为我已经在IE和Chrome中尝试过了,在我的.NET 3.5网站上都能正常工作。请确定你有sender.controltovalidate小写,因为我也有pascalcased ControlToValidate未定义。 – 2010-09-03 14:58:05
是的,试着像你一样,但仍然没有成功。你在声明自定义验证器标签时设置了哪些属性? – DJPB 2010-09-03 15:30:16