I'm using a jquery slideshow and I want to fill in the pictures from a database. I'm trying to use an ASP repeater to create the images in a div with this code.
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="dsSelectedCategory" Visible="True">
<ItemTemplate>
<a><asp:Image ID="Image2" runat="server" ImageURL="~/img1/photos/<%#Eval("PictureFilePath") %>" class="slide" /></a>
</ItemTemplate>
</asp:Repeater>
I'm getting the error that the server tag is not well formed and I'm guessing it is this bit
ImageURL="~/img1/photos/<%#Eval("PictureFilePath") %>"
PictureFilePath is a field in the database that holds the filename and extension of the file. So I have to write out the path to the file then add on the name.
Try
ImageURL='<%# "~/img1/photos/" + Eval("PictureFilePath") %>'
I have tried your code and found the answer. Finally, it solved my problem as well. Just mark as answer if worked for you.
This is your old code
ImageURL="~/img1/photos/<%#Eval("PictureFilePath") %>"
change it to
ImageURL='~/img1/photos/<%#Eval("PictureFilePath") %>'
replace the double quotes "" with single quotes ''
Related
What I am trying to achieve is to get the full path of the images, assigning them to the source attribute of the HTML image control. This image is placed within a Repeater Control.
Here the code:
<asp:Repeater runat="server" ID="flashImagesRepeater" >
<HeaderTemplate>
<table>
<tr id="bodyImagesTr" >
<td>
</HeaderTemplate>
<ItemTemplate>
<sc:Link ID="flashImageClickURL" runat="server" Field="Click Url" Item="<%# Container.DataItem %>" >
<img id="htmlImage" runat="server" field="Image" src="<%# MediaManager.GetMediaUrl(((ImageField)((Item)Container.DataItem).Fields["Image"]).MediaItem) %>" />
</sc:Link>
</ItemTemplate>
<FooterTemplate>
</td>
</tr>
</table>
</FooterTemplate>
Also made sure that I have the relevant namespace added <%# Import Namespace="Sitecore.Resources.Media" %> at the top.
But VS2010 gives me the error
Cannot resolve symbol 'MediaItem'
Can someone guide me as to what am I doing wrong?
May be the brackets are being being placed incorrectly?
Thanks for reading.
You definitely don't need to write anything in the code behind. It looks like for some reason one of the classes in you statement is considered as not from the one of the Sitecore namespaces. Try to use full class names with namespaces:
<img
id="htmlImage"
runat="server"
field="Image"
src="<%# Sitecore.Resources.Media.MediaManager.GetMediaUrl(((Sitecore.Data.Fields.ImageField)((Sitecore.Data.Items.Item)Container.DataItem).Fields["Image"]).MediaItem) %>"
/>
Looks like you're trying to render an image from a field in an item, just use the FieldRender control and let Sitecore handle it.
<sc:FieldRenderer runat="server" FieldName="Image" Item="<%# (Item)Container.DataItem %>" />
I think the problem you are having is that there is that ImageField is not resolving to the Sitecore image field correctly. I pasted that code into an aspx page and I got another error saying that the Field could not be cast as an ImageField. So I added the sitecore name space to the ImageField conversion and it properly intellisensed MediaItem as you wanted.
So change the image tag to this and it should work:
<img id="htmlImage" runat="server" field="Image" src="<%# MediaManager.GetMediaUrl(((Sitecore.Data.Fields.ImageField)((Item)Container.DataItem).Fields["Image"]).MediaItem) %>" />
Personally tho - I would use the Sitecore Image control to render the image, if you just want to find the media Url in back end code, the above code would work but could be written a lot more readable :)
Hope this helps!
I've been getting parser error with message The server tag is not well formed for the following line.
<asp:Repeater runat="server" DataSource="<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>">
<ItemTemplate>
<sc:FieldRenderer ID="FieldRenderer1" runat="server" FieldName="Tag name" Item="<%# Container.DataItem %>"/>
</ItemTemplate>
<SeparatorTemplate>
/
</SeparatorTemplate>
</asp:Repeater>
The syntax looks fine, but one thing I'm not sure about is whether you can use the ".Field["tags"] element in there.
I've tried looking it up, but couldn't find a similar problem. I'm hoping someone provide me with some insight to why the parser is complaining about this line.
Thanks
What comes into my mind right now is to use a single-quoted string instead:
<asp:Repeater runat="server" DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>' >
You have double quotes within the attribute. This confuses the parser - it can't tell where the attribute ends.
Wrap the attribute in single quotes to fix it:
DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>'
try ' instead of " it might work
else try binding from code behind
<asp:Repeater runat="server" DataSource='<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>' >
Do you have a closing tag? i.e.
</asp:Repeater>
Otherwise you are missing the / at the end of your tag declaration.
<asp:Repeater runat="server" DataSource="<%# ((MultilistField)((Item)Container.DataItem).Fields["Tags"]).GetItems() %>" />
<asp:HyperLink ID="TestHyperLink" runat="server" NavigateUrl='<%# "javascript:updateApplet();" %>' Text="Send" />
It says "The server tag is not well formatted."
Please help.
Missing ID Before "TestHyperLink"
<asp:HyperLink ID="TestHyperLink" runat="server" NavigateUrl='<%# "javascript:updateApplet();" %>' Text="Send" />
There are so many wrong things, have you ever, at least, bother to look in Google ?
<asp:HyperLink
id="TestHyperLink"
runat="server"
NavigateUrl="#"
OnClientClick="updateApplet()"
Text="Send" />
but for simple things like this, you don't need an ASP.NET Control, unless you are doing something from your code file... a simple <a> will do the trick.
Just understand that ASP.NET Controls are useful when we want to tweak or use then in our application as variables so we can control them dynamically. If we simply need a link that will never change, there is no need for a control, a html tag will do.
I have a public POCO property (SiteDetail) on my page and I need to know what's the best approach when setting properties of server controls:
Use inline code and Page.DataBind(); on load
<asp:Label ID="lbName" runat="server" Text="<%# SiteDetail.Name %>"/>
Do not use inline code and set control properties on page load
lbName.Text = SiteDetail.Name;
Is it "dangerous" to use Page.DataBind() on load?
Did you see the rendered source? There is no difference at all. Both labels will render text in the span. Just perform a simple test and it will be clear to you.
here is what I have for a test
<asp:Label ID="Label1" runat="server" Text='<%#test %>'></asp:Label>
<asp:Label ID="Label2" runat="server"></asp:Label>
here is generated source below.
<span id="Label1">this text is from binding expression</span>
<span id="Label2"><br/>this text set from code behind</span>
You will see no difference at all
So...I did some more research and found Page.DataBind() is not a good thing, is better to call DabaBind on every single control you need, as #Muhammad Akhtar says, both ways renders the same so I prefer to use inline code because it seems clearer, now I have
<asp:Label ID="lbName" runat="server" Text="<%# SiteDetail.Name %>"/>
and code behind:
if (!IsPostBack)
{
lbName.DataBind();
}
I think it might have been asked before but i was unable to find the right answer, so i am asking here. i have added a data source which is working fine, i wanted a feature where i query the top n entries from the database and add it with a hyperlink. Think of it like Latest News! The markup for the hyperlink inside the ItemTemplate of DataList is this.
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Eval("News_Id") %>' NavigateUrl="~/News.aspx?NewsId=<%#Eval("News_Id") %> " runat="server" /> </asp:HyperLink>
however i get The error as "Error Creating Control, Server tag is not well formed". It reports the error where the quotes are placed.
I know i can use datanavigateurl property but i want to write it in this way. as written in the markup above. How can i?
Upon re writing it to
NavigateUrl='~/Product.aspx?DVDID=<%#Eval("Title") %> '
i get the following as the url
http://localhost:61221/Product.aspx?DVDID=<%#Eval("Title") %>
try this :
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Eval("News_Id") %>'
NavigateUrl='<%#Eval("News_Id", "~/News.aspx?NewsId={0}") %>'
runat="server" />
</asp:HyperLink>
<%# Eval() %> must be inside single quotes, otherwise it throws error.
To concatenate string in your binding tag, you can use this :
<%# "~/News.aspx?NewsId=" + Eval("News_Id").ToString() %>