我想添加一个验证器来检查输入文本框的长度(<100个字符)。 从绑定列在Datagrid内部生成的文本框:
<asp:BoundColumn DataField="Comment" HeaderText="Comment"></asp:BoundColumn>
在后面的代码中,我在CustomizeItem函数中实现了自定义文本框:
private void CustomizeEditItem(DataGridItem item)
{
TextBox lCommentTextBox = ((TextBox)item.Cells[(int)DigitalCalendarGridEnum.COMMENT_COLUMN].Controls[0]);
//RegularExpressionValidator test = new RegularExpressionValidator();
//test.ID = "regularExpressionValidatorUnique";
//test.ControlToValidate = lCommentTextBox.UniqueID;//.Replace("$", "_");
//test.ValidationExpression = "^[\\s\\S]{0,100}$";
//test.ErrorMessage = "Please enter a maximum of 100 characters";
//test.Display = ValidatorDisplay.Dynamic;
//lCommentTextBox.Controls.Add(test);
lCommentTextBox.Width = Unit.Pixel(90);
lCommentTextBox.TextMode = TextBoxMode.MultiLine;
lCommentTextBox.Rows = 4;
}
在评论中,我尝试实现Validator,但出现错误:
Unable to find control id 'DigitalCalendarGrid$ctl12$ctl21' referenced by the 'ControlToValidate' property of 'regularExpressionValidatorUnique'.
我还尝试在aspx中实现验证器:
<asp:TemplateColumn HeaderText="Comment">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Comment") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbComment" runat="server" CssClass="inReq" AutoPostBack="True" TextMode="MultiLine" Rows="4" Width="90">
</asp:TextBox>
<asp:RegularExpressionValidator ID="CommentTextBoxValidator" runat="Server" ControlToValidate="tbComment" ValidationExpression="^[a-zA-Z0-9]^.{0,100}$" ErrorMessage="Comment should not exceed 100 characters" Display="None"></asp:RegularExpressionValidator>
</EditItemTemplate>
</asp:TemplateColumn>
但是:A它弄乱了我的网格(太大),B它不起作用。
我在哪里错了?