Asp.Net DataList bind array of *.aspx ImageUrls - asp.net

The code below works perfect for binding actual urls grabbed from the net. My issue is that it does not work for *.aspx urls that generate an image. If I go to the *.aspx page "~/UserPages/Photo/GetThumbnail.aspx?id=7", an image shows up just fine. However it does not work for the datalist. Any ideas why and how I can solve this issue. Thank you! The string array 'imageurls' consists of many *.aspx image urls.
<asp:DataList ID="dlImages" runat="server"
RepeatColumns="5"
RepeatDirection="Horizontal"
RepeatLayout="Flow">
<ItemTemplate>
<img src="<%# (string)Container.DataItem %>" />
</ItemTemplate>
</asp:DataList>
Code behind
protected void BindImages(string[] imageurls)
{
dlImages.DataSource = imageurls;
dlImages.DataBind();
}

Try
<%# ResolveUrl((string)Container.DataItem) %>

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 data binding in literal

Lets say we have the following in the default.aspx file
<asp:Literal runat="server" Text="<%= TestMethod() %>" />
What needs to be defined in the default.aspx.cs file to make this work?
I tried to add a method called TestMethod to the _Default class which simply returned the string Test, but it didn't seem to work.
Can anyone help?
Thanks,
AJ
Apart from the method being marked public...
i think you could also remove the asp:Literal altogether
example
your code
<p><asp:Literal runat="server" Text="<%= TestMethod() %>" /></p>
could be
<p><%= TestMethod() %></p>
However if you are intent on using the Literal then please rather set it on page load.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.aspx
Regards.
I think you can get the same result by doing this
In your .aspx file
<asp:Literal runat="server" ID="ltr1" />
And in your aspx.cs file
ltr1.text = TestMethod();

asp.net repeater with usercontrol - setting commandargument

I have the following repeater code:
<asp:Repeater ID="repMain" runat="server" OnItemCommand="repMain_ItemCommand" EnableViewState="false">
<ItemTemplate>
<dmg:testcontrol runat="server" MyData=<%#Container.DataItem %>>
</dmg:testcontrol>
</ItemTemplate>
</asp:Repeater>
The testcontrol usercontrol looks like:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="TestRepeater.TestControl" %>
<asp:Literal runat="server" ID="litMain" Text="<%#MyData.MyValue %>"></asp:Literal>
<asp:DropDownList runat="server" ID="dropdownMain"></asp:DropDownList>
<asp:Button runat="server" ID="btnMain" Text="Click Me" CommandName="Update" CommandArgument="<%#dropdownMain.SelectedValue%>"/>
Is it possible for me to send through the dropdownMain.SelectedValue as the CommandArgument?
Just now it is an empty string.
Thanks
Duncan
PS This is related to ASP.NET Repeater not binding after ItemCommand but I thought the two sufficiently different to keep apart.
A bit old question but I just my self found the answer to this one.
CommandArgument="<%#dropdownMain.SelectedValue%>"
Needs to look like this instead with single quotes! All inline codes within a asp.net controls have to be done this way instead.
CommandArgument='<%#dropdownMain.SelectedValue%>'
Why not get the selected value, and use it inside the Command function ?
(why to try to send it as argument, from the moment you can get it inside the command called function and its the same)

Form tag is not well formed (control Id)

Hi can anyone help me regarding my problem. I assigned a dynamic Id into the ID property of an image control. The problem is I got an error saying "form tag is not well formed". I replaced the double quote into a single or removed those quotes but I got the same error. How can I resolved this issue? By the way I used c# language.
<img ID="<%# DataBinder.Eval(Container.DataItem, "Id")%>" runat="server" />
Make this
<img ID="<%# DataBinder.Eval(Container.DataItem, "Id")%>" runat="server" />
to this:
<img ID='<%# DataBinder.Eval(Container.DataItem, "Id")%>' runat="server" />
Update: apparently it's indeed not possible to do this. I tried in a test application further with both the html image and Image server control. After thinking a bit further, and taking some coffee, it kind of makes sense that an ID cannot be set as such. How would you set properties on something without an ID in codebehind?
An alternative way what you can do however is this:
<form id="form1" runat="server">
<div>
<asp:PlaceHolder runat="server" ID="phTest"></asp:PlaceHolder>
</div>
</form>
and in codebehind:
HtmlImage image = new HtmlImage();
image.ID = "SomeRandomId";
image.Src = "urltosomeimage";
phTest.Controls.Add(image);
I got a similar problem and I solved it like this:
<img ID='<%# DataBinder.Eval(Container.DataItem, "Id")%>' runat="server" />
The reason for this is that the expression between <%# %> cannot be evaluated if it is not in single quotes.
How about this?
<asp:img ID='<%# DataBinder.Eval(Container.DataItem, "Id")%>' runat="server" />
or try to remove the runat="server" since your img tag is not a server control
If you're using .NET 4.0 set ClientIDMode=Static or change ID not in markup but in code-behind.
And use instead of if you need access it from code-behind.

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