簡體   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>

上面的Maxlength =“ 2”無效! 我什至嘗試了其他方法(如數據綁定),並使用foreach循環對其進行編碼,但仍然失敗。

用戶RegularExpressionValidator。 以下接受最小字符0和最大2個字符。

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

MaxLength在多行模式下不起作用。

您是否嘗試過<EditItemTeplate>而不是<ItemTemplate>嗎?

是的,這是因為TextMode="MultiLine" 您需要一些JavaScript來檢查字符總數和施加限制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM