简体   繁体   English

如何在GridView中限制文本框模板字段的文本长度?

[英]How to limit textbox template field text length in gridview?

            <asp:GridView ID="CommentGrid" GridLines="None" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField HeaderText="No." DataField="Num" >
                    <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField HeaderText="Subject Name" DataField="SubjectName">
                    <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="Comment">
                    <ItemTemplate>
                        <asp:TextBox ID="CommentBox" runat="server" TextMode="MultiLine" CssClass="commentTbx" MaxLength="2"></asp:TextBox>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Left" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

The above Maxlength="2" doesnt work!! 上面的Maxlength =“ 2”无效! I even tried other method like databound, and foreach loop to code it but it still fails. 我什至尝试了其他方法(如数据绑定),并使用foreach循环对其进行编码,但仍然失败。

User RegularExpressionValidator. 用户RegularExpressionValidator。 The following accepts minimum characters 0 and maximum 2 characters. 以下接受最小字符0和最大2个字符。

  <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
        ControlToValidate="CommentBox" ErrorMessage="Out of Range Max 2 Chars" 
        ValidationExpression=".{0,2}"></asp:RegularExpressionValidator>

MaxLength does not work on MultiLine mode. MaxLength在多行模式下不起作用。

您是否尝试过<EditItemTeplate>而不是<ItemTemplate>吗?

Yes, it is because of TextMode="MultiLine" . 是的,这是因为TextMode="MultiLine" You need some javascript to check total chars and impose limit. 您需要一些JavaScript来检查字符总数和施加限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM