ASP.NET-細(xì)節(jié)-GridView中顯示的列內(nèi)容里字符太長

GridView中顯示的列內(nèi)容里字符太長,怎樣顯示截取部分字符并以"..."結(jié)尾
之前的項目中遇到過一個問題,就是gridview中,綁定數(shù)據(jù)源以后,有部分字段內(nèi)容過長,導(dǎo)致表格被撐開變形
當(dāng)時為了排除這個問題,查了一些方法,最終選擇了一種我覺得比較好用的方法,就是在RowDataBound的時候?qū)懘a進(jìn)行處理
下面是我的前臺和后臺代碼,特此記錄:
前臺js文件:

<table cellpadding="0"cellspacing="0"class="box_fold"  runat="server" >
   <trclass="box_fold_tr">
       <tdnowrap="noWrap"style="padding-left: 5px;">
            <a href="javascript:void(0);" onclick="iofoldset(this,'ctl00_Content_tblFold6');">
                <img src="/iOffice/img/idt_tle_c.gif" border="0" align="absbottom"alt=""/></a>
            <strong id="stOtherEducates" runat="server">其它學(xué)歷信息</strong>
       </td>
       <tdalign="right"nowrap="nowrap"style="height: 20px; padding-right: 20px">
            <asp:LinkButton ID="lnkAddEducate" runat="server" CssClass="td"><imgsrc="/iOffice/img/add.gif"border="0"align="absmiddle"alt="添加學(xué)歷信息">添加學(xué)歷</asp:LinkButton>
       </td>
   </tr>
   <tr>
       <tdclass="box_td"colspan="2">
            <table id="tblFold6" runat="server" cellspacing="0" cellpadding="0" width="580px">
                <tr>
                    <td>
                        <asp:DataGrid ID="dgdEducate" runat="server" AutoGenerateColumns="False" DataKeyField="ID" Width="95%">
                            <Columns>
                                <asp:TemplateColumn HeaderText="序號">
                                    <HeaderStyle Width="40px"Wrap="False"/>
                                    <ItemStyle HorizontalAlign="Center"/>
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server"Text='<%#DataBinder.Eval(Container, "ItemIndex")+1 %>'>
                                        </asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                                <asp:HyperLinkColumn DataNavigateUrlFormatString="javascript:openpage('hrepEduBackground.aspx?EduBGID={0}',690,615,'EducateAddUp')" DataNavigateUrlField="ID"DataTextField="School"HeaderText="學(xué)習(xí)單位/學(xué)校">
                                    <HeaderStyle Wrap="False"/>
                                    <ItemStyle Wrap="False"/>
                                </asp:HyperLinkColumn>
                                <asp:BoundColumn DataField="EduCate"HeaderText="學(xué)歷">
                                    <HeaderStyle Wrap="False"/>
                                    <ItemStyle Wrap="False"/>                                                        
                                </asp:BoundColumn>
                               <asp:BoundColumn DataField="HighestMajor"HeaderText="專業(yè)">
                                    <HeaderStyle Wrap="False"/>
                                    <ItemStyle Wrap="False"/>
                                </asp:BoundColumn>
                                <asp:BoundColumn DataField="HTeacherName"HeaderText="導(dǎo)師">
                                    <HeaderStyle Wrap="False"/>
                                    <ItemStyle Wrap="False"/>
                                </asp:BoundColumn>
                                <asp:BoundColumn DataField="sfdate"HeaderText="開始時間">
                                    <HeaderStyle Wrap="False"/>
                                    <ItemStyle Wrap="False"/>
                                </asp:BoundColumn>
                                <asp:BoundColumn DataField="stdate"HeaderText="結(jié)束時間">
                                    <HeaderStyle Wrap="False"/>
                                    <ItemStyle Wrap="False"/>
                                </asp:BoundColumn>
                                <asp:BoundColumn DataField="HEducationSector"HeaderText="教育類別">
                                    <HeaderStyle Wrap="False"/>
                                    <ItemStyle Wrap="False"/>
                                </asp:BoundColumn>
                                <asp:BoundColumn DataField="HProfessionalType"HeaderText="專業(yè)類型">
                                    <HeaderStyle Wrap="False"/>
                                    <ItemStyle Wrap="False"/>
                                </asp:BoundColumn>
                                <asp:TemplateColumn>
                                    <ItemStyle />
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkRemove"runat="server"CausesValidation="false"CommandName="del" Text="<imgsrc=/iOffice/img/delete.gif border=0 alt=刪除 />"></asp:LinkButton>
                                    </ItemTemplate>
                                    <HeaderStyle Width="1%" />
                                </asp:TemplateColumn>
                            </Columns>
                        </asp:DataGrid>
                    </td>
                </tr>
            </table>
       </td>
   </tr>
</table>

后臺代碼:
首先是ItemDataBound函數(shù)的編寫,

Private Sub dgdEducate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdEducate.ItemDataBound
       If e.Item.Cells.Count - 1 > 0 Then
            Dim i As Integer
            For i = 0 To e.Item.Cells.Count - 1
                Ife.Item.Cells(i).Text <> "" Then
                    e.Item.Cells(i).Text =newstring(e.Item.Cells(i).Text.ToString, 10)
                EndIf
            Next
       EndIf
End Sub

其中涉及到一個我自己寫的字符串處理函數(shù)下面是源碼:

Private Function newstring(ByVal str AsString, ByValstrlen As Integer)as string
       If Len(str) > strlen Then
            Ifstrlen > 5 Then
                str = Mid(str, 1, strlen - 5) +"..."
            Else
                str = Mid(str, 1, 3) + "..."
            EndIf
       End If
       Return str
End Function

當(dāng)然還有另一種處理方法,就是在數(shù)據(jù)庫獲取的時候就進(jìn)行處理,這種方法也很好用,下面是源碼:
company=case when len(company)>10 then left(company,10)+'...' else company end
其中Company就是列名,后面發(fā)現(xiàn)了這種數(shù)據(jù)庫處理方法以后,我基本都用這種方法進(jìn)行處理了,因為實在是比在后臺里處理方便快捷太多

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,715評論 0 5
  • 前言 暑假我在學(xué)校學(xué)習(xí)了一個月的時間 ,在這一個月期間我學(xué)習(xí)了關(guān)于安卓移動前端開發(fā)的一些簡單的知識。之前我對安卓移...
    W_Nicotine閱讀 11,896評論 1 10
  • 所有知識點已整理成app app下載地址 J2EE 部分: 1.Switch能否用string做參數(shù)? 在 Jav...
    侯蛋蛋_閱讀 2,710評論 1 4
  • 轉(zhuǎn)原創(chuàng)微小說:委屈 去年底經(jīng)多道工序,我被提縣府辦秘書科長在縣府大院張榜公示。后不知什么原因被擱淺了。經(jīng)多...
    吳旭明閱讀 155評論 0 0
  • 頁碼P344-358 一、有意義的人際關(guān)系 集體力量遠(yuǎn)遠(yuǎn)大于個人的能力,個人要參與團(tuán)體協(xié)作 首先要認(rèn)同公司的價值和...
    獸獸ran閱讀 266評論 0 1

友情鏈接更多精彩內(nèi)容