后台方法处理:
/// <summary> /// 将指定字符串按指定长度进行剪切, /// </summary> /// <param name= "oldStr "> 需要截断的字符串 </param> /// <param name= "maxLength "> 字符串的最大长度 </param> /// <param name= "endWith "> 超过长度的后缀 </param> /// <returns> 如果超过长度,返回截断后的新字符串加上后缀,否则,返回原字符串 </returns> public static string StringTruncat(string oldStr, int maxLength, string endWith) { if (string.IsNullOrEmpty(oldStr)) // throw new NullReferenceException( "原字符串不能为空 "); return oldStr + endWith; if (maxLength < 1) throw new Exception("返回的字符串长度必须大于[0] "); if (oldStr.Length > maxLength) { string strTmp = oldStr.Substring(0, maxLength); if (string.IsNullOrEmpty(endWith)) return strTmp; else return strTmp + endWith; } return oldStr; }
页面数据绑定时处理:
<a href="<%# Eval(" mce_href="<%# Eval("WEBURL") %>" width="100%" target="_blank" id="WEBURL1"><%# Eval("WEBURL").ToString().Length > 22 ? Eval("WEBURL").ToString().Substring(0, 22)+"..." : Eval("WEBURL").ToString()%></a>
<dxe:ASPxImage runat="server" ID="IMGURL2" ImageUrl='<%#Eval("IMGURL","~/bigregionimages/{0}") %>' Height="150px" Width="150px"></dxe:ASPxImage>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="Id" onrowdeleting="GridView1_RowDeleting"> <RowStyle HorizontalAlign="Center" /> <Columns> <asp:BoundField DataField="Id" HeaderText="编号" /> <asp:BoundField DataField="Title" HeaderText="标题" /> <asp:BoundField DataField="AddTime" HeaderText="添加时间" /> <asp:TemplateField HeaderText="内容"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Content").ToString().Length>25?Eval("Content").ToString().Substring(0,25):Eval("Content").ToString() %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="所属栏目"> <ItemTemplate> <a href='Admin_Article.aspx?Id=<%# Eval("ColumnId") %>'><%#Eval("ColumnName") %></a> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="状态"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Eval("State").ToString()=="0"?"显示":"隐藏" %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="允许评论"> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Eval("AllowRevert").ToString()=="0"?"是":"否" %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="置顶"> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Eval("AllowSetTop").ToString()=="0"?"是":"否" %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="操作"> <ItemTemplate> <a href='Admin_AddFriend.aspx?Id=<%# Eval("Id") %>'>编辑</a> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="删除" CssClass="delete-link"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
将字符串中的数字去除掉:
string name = System.Text.RegularExpressions.Regex.Replace(Session["UserName"].ToString(), "\\d", "");