2013-09-28 67 views
0

名的动态列表我在GridView下面的代码片段:显示在工具提示

<ItemTemplate> 
<asp:Label 
    ID="lblRecipientsCount" 
    runat="server" 
    Text='<%#String.Join(",",((Share.Service.Message)Container.DataItem).Recipients.Count()) %>' 
    ToolTip='<%#String.Join(","((Share.Service.Message)Container.DataItem).Recipients.ToString()) %>'> 
</asp:Label> 
</ItemTemplate> 

的计数收件人正常工作数据绑定标签。当我将第二个绑定标签添加到工具提示时,工具提示将显示Share.Service.Person []。

我的最终目标是让工具提示显示逗号分隔的收件人姓名列表。

任何帮助表示赞赏。

回答

0

我会简单地使用RowDataBound

protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     var message = (Share.Service.Message)e.Row.DataItem; 
     var lblRecipientsCount = (Label)e.Row.FindControl("lblRecipientsCount"); 
     lblRecipientsCount.Text = message.Recipients.Count(); 
     lblRecipientsCount.ToolTip = string.Join(",", message.Recipients.Select(rec => rec.Name)); 
    } 
} 

数据绑定表达式不支持复杂的lambda表达式(据我所知)。

Codebehind也更具可读性,可维护性并且不易出错。

+0

On Count,我得到了:属性'System.Array.Count'不能在这个上下文中使用,因为get访问器是不可访问的。 –

+0

@Rex_C:如果是数组(编辑),则使用'Length'或'Count()'。 –

+0

现在,它在Select Select上抛出一个错误,并且它无法解析我的.Name属性。 –