首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP Gridview MaxLength

ASP Gridview MaxLength
EN

Stack Overflow用户
提问于 2013-01-30 04:10:11
回答 1查看 2.4K关注 0票数 0

我有一个使用<asp:BoundField DataField="Comments" HeaderText="COMMENTS" />的网格视图,当网格视图填充时,我想只显示Commemnt列中的前20个字符。有没有办法在VB中做到这一点?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-30 04:16:17

一种方法是在代码背后使用RowDataBound事件:

代码语言:javascript
复制
Protected Sub Gridview1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles Gridview1.RowDataBound
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow
            ' assuming the comments column is the first column '
            If e.Row.Cells(0).Text.Length > 20 Then
                e.Row.Cells(0).Text = e.Row.Cells(0).Text.Substring(0, 20)
            End If
    End Select
End Sub

请注意,您只能使用BoundFields以这种方式访问文本。使用TemplateFields时,您需要使用FindControl来获取控件的引用(F.E.一个TextBox)。

如果要使用TemplateField,还可以限制aspx标记上的文本:

代码语言:javascript
复制
<asp:TemplateField HeaderText="Commnents">
<ItemTemplate>
    <asp:TextBox ID="txtID"  
         MaxLength="20" runat="server" 
         Text='<%# DataBinder.Eval(Container.DataItem, "Comments") %>'>
    </asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14591343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档