0

Actually I am binding some data on tooltip of a Image control in gridview.

all is working fine. see my code below

<asp:Image ID="Image1" runat="server" ImageUrl="~/img/images.jpg" ToolTip='<%# (Eval("Comments").ToString().Length == 0? "": "Late Reason - " + DataBinder.Eval(Container.DataItem, "Comments")) + (Eval("Out_Comments").ToString().Length == 0? "" :"| Out Reason - "+ DataBinder.Eval(Container.DataItem, "Out_Comments")) %>' />

my requirement is that when Comments field is null or blank in database then this extra text "Late Reason - " should remove from the tool tip and if the Comments field is not blank or null then this extra text should be there in tooltip. And it also does same for the Out_Comments field which is having extra text "Out Reason - ".

how can I do this?

enter image description here

Gaurav
  • 557
  • 4
  • 11
  • 28
  • One solution would be just dont show the tooltip if field is blank or null. – Microsoft DN Jun 05 '13 at 06:59
  • there are two fields Comments and Out_Comments. I can hide tooltip if both are blank or null. What if there is blank but another is not? – Gaurav Jun 05 '13 at 07:06

2 Answers2

1

Try this:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Image ID="Image1" runat="server" ImageUrl="~/img/images.jpg"
            ToolTip='<%# !string.IsNullOrEmpty(Eval("Comments") as string)
        ? !string.IsNullOrEmpty(Eval("Out_Comments") as string)
          ? "Late Reason - " + Eval("Comments") + " | Out Reason - " + Eval("Out_Comments")
          : "Late Reason - " + Eval("Comments")
        : "Out Reason - " + Eval("Out_Comments")%>' />
    </ItemTemplate>
</asp:TemplateField>

Edit: changed the null checking to string.IsNullOrEmpty(Eval("Comments") as string) to check for empty/blank strings.

Ben Gulapa
  • 1,619
  • 14
  • 10
  • not working. by the why where is image and it's tool tip property? – Gaurav Jun 05 '13 at 07:25
  • now see I have attached an image. Your solution is not working.according to Your solution if Comments is null or blank although it is showing "Late Reason - " but my requirement is that it shouln't show this extra text. – Gaurav Jun 05 '13 at 08:44
0

You should be handling this logic in the GridView RowDataBound Event.

Image img = e.Row.FindControl("Image1");

if( img != null ) {

    //Apply Logic Here

    img.ToolTip = //End result of logic
}
fnostro
  • 4,531
  • 1
  • 15
  • 23