Display image based on a query result in asp GridView - asp.net

i am looking for a way that:
If the Gridview contains a result, don't show the result but instead an clickable image that will direct me to a new page with the result(this i guess in the properties of the image).
And if the gridview is empty, then display another image.
I know how to do it if the gridview is empty i use
But i would like to know how to show an image if gridview is not empty.
Any ideas?
Thanks

<a href='<%# String.Format("{0}{1}{2}{3}{4}{5}{6} {7}8}",Eval("CourseName"),"/","Colleges","/",Eval("StateName").ToString().Replace(" ", "-"),"/",Eval("CityName").ToString().Replace(" ", "-"),"/",Eval("CollegeName").ToString().Replace("-", "=").Replace(" ", "-").Replace("..","--")) %>' rel="canonical" title='<%# Eval("CollegeName")%>'>
<img Height="50px" ImageUrl='<%# String.Format("{0}{1}","\\images\\CollegeImages\\" , string.IsNullOrEmpty(Eval("CollegeImage").ToString()) ?"noImageCollege.jpg":Eval("CollegeImage")) %>' Width="50px" alt='<%# Eval("CollegeName") %>' title='<%# Eval("CollegeName") %>' />
</a>

Related

Image not getting displayed in datalist

I am binding images to datalist. Taking the image name from database and giving the path.
My code is:
<asp:DataList ID="dlImages" runat="server" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Height="200px" Width="200px" ImageUrl='<%# Eval("PageName","D:\Sagar\Kinston\WebSite\ScreenMasterImages\{0}.jpg") %>' runat="server" />
</ItemTemplate>
</asp:DataList>
On .cs page:
ds = gc.GetDataToListBinder("select DISTINCT PageOrderID,PageName from ScreenMaster order by PageOrderID")
dlImages.DataSource = ds.Tables(0)
dlImages.DataBind()
I am facing 2 problems :
When imagename has space in between it adds %20 in between
Eg. if imagename is "API Message", it takes it as: "API%20Message"
I tried On this Problem:
Added ImageUrl='<%#Server.HtmlDecode(Eval("PageName","D:\Sagar\Kinston\WebSite\ScreenMasterImages\{0}.jpg"))'
But I got error:
XML literals and XML properties are not supported in embedded code within ASP.NET
If there is not space Eg.image name is "Charges" , Then also its not showing it in datalist.
When i ran project, and right clicked on it and view source, then its showing me correct path as:
src="D:\Sagar\Kinston\WebSite\ScreenMasterImages\Charges.jpg"
but not showing image.
Please help me with above code.
Where i have made mistake?
What else i should add in it?
Keep it easy and simple.
When trying things like that with URL, to know exactly what to write down, try typing it in your address bar so you'll be able to find the exact syntax needed in order to make it work correctly.
I've done this tons of time and work for me... So you could try something like :
<asp:DataList ID="dlImages" runat="server" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Height="200px" Width="200px"
ImageUrl='<%# String.Format("{0}{1}.jpg", "~\ScreenMasterImages\", Server.HtmlDecode(Eval("PageName"))) %>'
runat="server" />
</ItemTemplate>
</asp:DataList>

asp.net gridview edit template http link

I have a label within an Edit Template for a gridview which goes like this:
<asp:Label ID="lblProjectID" runat="server" Text='<%# Bind("Project_ID") %>'></asp:Label>
I would like to turn that label text into a http link like so:
<asp:Label ID="lblProjectID" runat="server" Text='<a href=http://intranet/?<%# Bind("Project_ID") %>> <%# Bind("Project_ID") %></a>'></asp:Label>
So the link would look something like http://intranet/?Project_ID
But that syntax is incorrect. What is the correct way to write that?
This is what you can do on your Label tag.
<asp:TemplateField>
<ItemTemplate>
<a href='<%# String.Format("http://intranet/?Project_ID={0}", Eval("Project_ID")) %>'><%# Eval("Project_ID")%></a>
</ItemTemplate>
</asp:TemplateField>
If you want it to be a link... then just use a link, not a label:
<a href='http://intranet/?<%# Eval("Project_ID") %>'><%# Eval("Project_ID")%></a>
or same thing with HyperLinkField (if you want to use it as a column and not inside EditItemTemplate:
<asp:HyperLinkField DataTextField="Project_ID" DataNavigateUrlFields="Project_ID" DataNavigateUrlFormatString="http://intranet/?{0}" />

Which property or method should i use to bind the ToolTip property to an ImageField Class?

In an image control that is bound within a GridView column i can set the Alt text property by using the DataAlternateTextField property.
<asp:ImageField DataImageUrlField="Flag"
DataImageUrlFormatString="~/images/f/{0}.png"
DataAlternateTextField="Description" >
</asp:ImageField>
which results in somathing like...
<img src="1.png" alt="Alt text"/>
Which property or method should i use to bind the ToolTip property?
<img src="1.png" alt="Alt text" ToolTip="Title text" />
I would like the users of the website, on mouse hovering of the image, to be able to read the title property.
To all newbies out there just like me, here what i did
<asp:TemplateField HeaderText="Test" SortExpression="">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# Eval("Flag", "~/images/f/{0}.png") %>'
AlternateText='<%# Bind("Description") %>'
ToolTip='<%# Bind("Description") %>' />
</ItemTemplate>
</asp:TemplateField>
Thanks Veronica
You can use binding expression to set the title of the ImageField. http://msdn.microsoft.com/en-us/library/ms178366.aspx
I am playing with this issue now according to Microsoft, the alternate text property of an ImageField should translate into a tooltip as well. At least that's what it says on the msdn page i just linked.

asp buttonimage alternate text not visible

I've a problem with asp:image and asp:imagebutton alternate text: it's not visible when I move the mouse over the image. I declared the object in that way:
<asp:ImageButton ID="ButtonStatus"
ImageUrl='<%# IIF(Eval("Active")=1,"../images/folders/actived.png","../images/folders/deactivate.png")%>' CommandName="ChangeStatus" CommandArgument='<%# Eval("id_repository") %>' OnCommand="FolderCommand"
Enabled = '<% #iif(mySecurity.Admin=1, "True", "False") %>' Width="24px" AlternateText="Change the status"
runat="server" />
and for asp:image
<asp:HyperLink runat="server" ID="url_groups" NavigateUrl="~/action/Group_manager.aspx?action=2">
<asp:Image id="img_groups" runat="server" ImageUrl="~/Images/folders/group_config.png" AlternateText="Group manager"
EnableViewState="false" ImageAlign="Middle" Width="32px" CssClass="navigation" />
</asp:HyperLink>
Could you help me? I tried to search on web but I wasn't able to find any post or suggestion to fix that problem
thanks,
Andrea
It's browser dependent whether the alt text gets shown when you mouse over an image. If you set the ToolTip parameter on a .NET Image or ImageButton it will render a title attribute which shows across pretty much every browser.
Here's an example from a site I'm currently working on:
<asp:HyperLink ID="ReplyLink" runat="server" NavigateUrl='<%# Eval("SrcAddr", "~/msp/send.aspx?to={0}") %>'>
<asp:Image ID="ReplyIcon" runat="server" ImageUrl="~/images/email_go.png" AlternateText="Reply" ToolTip="Reply" />
</asp:HyperLink>
I think you're mixing up the alt attribute with the title attribute. Alt is shown when the user doesn't accept images and for screen readers whereas the title attribute is shown as tool tip when you hover the image.

Asp.Net Image in datalist onclick event? is it possible?

Ok I have been working on this gallery for some time and I keep getting little tidbits. It is now time to finish it off. I have a datalist which binds to an array of *.aspx image urls that contain a thumbnail id that is sent through the url. I now need to implement an onclick event, that when a user clicks on a photo, it brings them to the actual picture.
example url:
(thumbnail) = ~/UserPages/Photo/GetThumbnail.aspx?id=7
(actualpic) = ~/UserPages/Photo/GetPhoto.aspx?id=7
What I need: How do i get it so that each photo has an onclick event? I tried adding onclick to the imag src but it didn't work. It is difficult because it is not an actual image control, they exist inside a datalist. I also need to know how to extract the thumbnail url when getting clicked so that I can grab the id and redirect to the actual picture. Help please!
<asp:DataList ID="dlImages" runat="server"
RepeatColumns="5"
RepeatDirection="Horizontal"
RepeatLayout="Flow">
<ItemTemplate>
<img src="<%# ResolveUrl((string)Container.DataItem) %>" />
</ItemTemplate>
</asp:DataList>
Code Behind:
dlImages.DataSource = ImageUrls;
dlImages.DataBind();
Can you wrap it in an a tag?
<ItemTemplate>
<img src="<%# ResolveUrl(String.Format("~/UserPages/Photo/GetThumbnail.aspx?id={0}", Container.DataItem)) %>" />
</ItemTemplate>
This assumes your DataItem contains only the ID.
Try:
<ItemTemplate>
<img src="<%# ResolveUrl((string)Container.DataItem) %>" onclick="doSomething(this)" />
</ItemTemplate>
After that you can simply implement a doSomething function that parses the id out of "this.src" and do whatever you want with it.

Resources