Databinder.Eval 和子字符串 [英] Databinder.Eval and Substring

查看:32
本文介绍了Databinder.Eval 和子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用中继器控件和数据绑定器将数据从数据库显示到我的网站.示例:DataBinder.Eval(Container, "DataItem.title")

Im using a repeater control and a databinder to display data from the database to my website. example: DataBinder.Eval(Container, "DataItem.title")

有时文字太长通常我使用子字符串来显示首选字符串的长度.但是我如何使用数据绑定器做到这一点如果文本太长(> 20 个字符),我想截断它并留下三个点.怎么做?

Sometimes the text is too long Normally I use substring to display the preferred string in length. But How do I do this with the databinder And if the text is too long (> 20 characters) I want to truncate it and leave three dots behind. How to do that ?

推荐答案

我建议使用扩展方法来完成繁重的工作,以使标记尽可能简单:

I'd suggest an extension method to do the heavy lifting, in order to make the markup as simple as possible:

public static string EvalTrimmed(this RepeaterItem container, string expression, int maxLength)
{ 
    string value = DataBinder.Eval(container, expression) as string;
    if ( value != null ) 
       return null;
    if (value.Length > maxLength)
       value = value.Substring(0,maxLength) + "...";
    return value;
}

然后在标记中使用它:

<%# Container.EvalTrimmed("DataItem.Title", 20) %>

这篇关于Databinder.Eval 和子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆