Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then If e.Item.Cells(0).Text.Length > 26 Then e.Item.Cells(0).Attributes.Add("Title", e.Item.Cells(0).Text) e.Item.Cells(0).Text = e.Item.Cells(0).Text.Substring(0, 26) + "…" End If e.Item.Cells(1).Text = Format(System.Convert.ToDateTime(e.Item.Cells(1).Text),_ "yyyy年M月d日 h点m分s秒") End If End Sub
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
If e.Item.Cells(0).Text.Length > 26 Then
e.Item.Cells(0).Attributes.Add("Title", e.Item.Cells(0).Text)
e.Item.Cells(0).Text = e.Item.Cells(0).Text.Substring(0, 26) + "…"
End If
e.Item.Cells(1).Text = Format(System.Convert.ToDateTime(e.Item.Cells(1).Text),_
"yyyy年M月d日 h点m分s秒")
End If
End Sub
{
if(e.Item.ItemType==ListItemType.EditItem)
{
for (int i = 0;i<16;i++)
{
TextBox textBox = (TextBox)e.Item.Cells[i].Controls[0];
textBox.Width = 200%;
}
}
}
然后在后台写方法,实现截断字符串
前台<%#getString((string)(DataBinder.Eval(Container.DataItem,"title")),38)%>
后台: protected string getString(string StringInput,int StringLength)
{
if(StringInput==""||StringInput==null)
return "";
string tempTitle="";
if(StringInput.Length>StringLength)
tempTitle = StringInput.Substring(0,StringLength)+"...";
else if(StringInput.Length==StringLength)
tempTitle = StringInput;
else
{
for(int i=0;i<StringLength-StringInput.Length;i++)
{
StringInput = StringInput+" ";
tempTitle=StringInput;
}
}
return tempTitle;
}
将一个汉字长度看作1,一个英文字母长度也是1...........有什么函数能将一个汉字长度看作2的,英文一个才是1 ?汉字占的位置可是英文字母的两倍啊~
if(buf.Length > 30)
{
e.Item.Cells[cellIndex].Text = System.Text.Encoding.Default.GetString(buf, 0, 30 - 1) + "...";
}
<FONT face="宋体">
<asp:TextBox id=TextBox5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.fcontactname") %>' CssClass="myinput">
</asp:TextBox></FONT>
</ItemTemplate>