对于任何感兴趣的人,这是我对 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。