大家好请教个问题(关于怎样使用DataList 对绑定数据的操作)
原代码如下:
<asp:DataList ID= "datresult " runat= "server " Width= "636px " OnItemCreated= "datresult_ItemCreated " OnItemDataBound= "datresult_ItemDataBound ">
<ItemTemplate>
<table>
<tr>
<td style= "width: 186px; height: 17px; ">
</td>
<td style= "width: 557px; height: 17px; ">
<asp:Label ID= "lbltitle " runat= "server " Width= "533px "> <%#Eval( "CON_TITLE ")%> </asp:Label> </td>
</tr>
<tr>
<td style= "width: 186px; height: 6px; ">
</td>
<td style= "width: 557px; height: 6px; ">
<asp:Label ID= "lbldesc " runat= "server " Width= "535px "> <%#Eval( "CON_DESC ")%> </asp:Label> </td>
</tr>
<tr>
<td colspan= "2 " style= "height: 2px ">
<hr />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
我想通过Eval函数绑定数据,但我想对其中之一的数据进行截断字符串的操作,我的操作语句如下:
protected void datresult_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.FindControl( "lbldesc ") as Label != null)
{
if ((e.Item.FindControl( "lbldesc ") as Label).Text.Length > 30)
{
(e.Item.FindControl( "lbldesc ") as Label).ToolTip = (e.Item.FindControl( "lbldesc ") as Label).Text;
(e.Item.FindControl( "lbldesc ") as Label).Text = (e.Item.FindControl( "lbldesc ") as Label).Text.Substring(0, 30) + "... ";
}
}
}
可是根本不起任何作用,请教下如何操作,我的语句对的吗?
还有就是如何实现针对有条件模糊查询的关键字查询,我要让查询结果类似于baidu搜索那样,将查询的匹配词条以红色显示,急着等答案,谢谢大家了
[解决办法]
ItemDataBound事件里
e.Item.Cells[0].Text = "· ";
e.Item.Cells[1].ToolTip = e.Item.Cells[1].Text;
e.Item.Cells[1].Text= " <a href=ShowNews.aspx?newsid= " + e.Item.Cells[2].Text + "> " + e.Item.Cells[1].Text.Length > 18 ? e.Item.Cells[1].Text.Substring(0,18) + "... ":e.Item.Cells[1].Text) + " </a> ";
[解决办法]
<%# 函数名(Eval( "CON_DESC ").ToString())%>
CS里
函数名(string str)
{
if(str.Length> 10)
return str.subString(0,18);
else
return str;
}
[解决办法]
public void 函数名(string str)
[解决办法]
两种方法都不行`?
[解决办法]
你怎么写的
贴出来看看
[解决办法]
SubStr 大小写`
[解决办法]
<%#Substr(Eval( "CON_DESC ").ToString())%>