您可以使用三元运算符?
:
<asp:Label ID="CountryNameLabel" runat="server"
Text='<%# Eval("CorporateAddressCountry").ToString().Length <= 10 ? Eval("CorporateAddressCountry") : Eval("CorporateAddressCountry").ToString().Substring(0,10) %>' >
</asp:Label>
另一种在我看来更具可读性的方法是使用 GridView 的RowDataBound
事件:
protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var row = (DataRowView) e.Row.DataItem;
var CountryNameLabel = (Label) e.Row.FindControl("CountryNameLabel");
String CorporateAddressCountry = (String) row["CorporateAddressCountry"];
CountryNameLabel.Text = CorporateAddressCountry.Length <= 10
? CorporateAddressCountry
: CorporateAddressCountry.Substring(0, 10);
}
}