I'm pretty new to using XML and very new to XPath.
I'm trying to select a field in my XML document and display that in a label on a webpage. Sound nice and simple.
Here is the code on my webpage:
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="NewsListView" runat="server" DataSourceID="NewsDataSource">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" ><%#XPath("temp1")%></asp:Label>
</ItemTemplate>
</asp:ListView>
<asp:XmlDataSource ID="NewsDataSource" runat="server" DataFile="~/news.xml"></asp:XmlDataSource>
</div>
</form>
</body>
This works 100% fine when using a XML document like the one below.
<test>
<response>
<temp1>23.7</temp1>
</response>
</test>
But the XML document I am trying to access, which I cannot change looks like this:
<response>
<temp1>23.7</temp1>
</response>
Basically the same document but without the extra node at the top.
Is there anyway I can get XPath to read the value of temp1 without the extra node?
Thanks.
Im not familiar with xpath but looking at documentation.. can you add
<asp:Repeater
id="Repeater1"
runat="server"
DataSource='<%# XPathSelect("response") %>' >
Found a solution. Adding XPath="/response" to the XMLDataSource did the trick.
I have never tried this API before, but does it work to do something like this?
<%#XPath("temp1/text()")%>
This xpath should return the text of the temp1 element.
Related
I have this code
<asp:CheckBox ID="ddd" Text='Accept terms runat="server">
This adds a link on "Accept terms". But I only want the link to be on the word "terms". I have tried different stuff, but none works. Most of the examples I found on the internet, were where the strings where pulled up from the database, and were not static. That didnt work. Basically I want this:
<asp:CheckBox ID="ddd" Text='Accept' + 'terms runat="server">
Notice the Accept + terms.
Another struggle I have, is that this same code:
<asp:CheckBox ID="ddd" Text='Accept terms runat="server">
makes the checkbox and text "Accept terms" be on 2 lines, but I want them on one line. What am I doing wrong?
You're almost there
<asp:CheckBox ID="ddd" Text='Accept terms'
runat="server"/>
You missed a ' at the end of you Text attribute. I've also moved the anchor to term as it seems that's what you're looking for.
Change your code to:
<asp:CheckBox ID="ddd" Text='Accept terms' runat="server" />
This should work just tested it.
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!
Maybe I've forgotten how to use these, but I am going crazy trying to inject a server-side value into an HTML output. There are reasons why I am doing this inline, and not server-side, so please don't suggest that as a solution.
This code on the server side:
<asp:Label ID="Label1" runat="server" Text='<%= DateTime.Now.ToString() %>' />;
Renders as this in the client HTML sent to the browser:
<span id="Label1"> <%= DateTime.Now.ToString()></span>;
And it displays as big fat empty space, and nothing output to the interface.
If I change the ASP source to using the "#" character to define as data-binding syntax, then the rendered output to browser becomes:
<span id="Label1"></span>
EDIT:
Setting Label text was just a simplified object for the sake of asking the question. In real life, I am setting the CssClass attribute, which does not allow me to use the "wrapping" workaround some have suggested. I wanted to set a public property and have all the controls update from it dynamically on page load.
Ideally, since I already have all the controls laid out on the aspx page. Just looking to add an attribute. I wanted to have:
<asp:textbox ID='MyTxtBox1' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox2' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox3' CssClass='<% strOtherVal %>' />
<asp:textbox ID='MyTxtBox4' CssClass='<% strVal1 %>' />
Now what it looks like I need to do is repeat all my (250+) controls on the codebehind in a block of code that looks like:
MyTxtBox1.CssClass=strVal1
MyTxtBox2.CssClass=strVal1
MyTxtBox4.CssClass=strVal1
MyTxtBox3.CssClass=strOtherVal
I believe that may not work on a compiled Web Application as it's not interpreted at run-time like a C# "Web Site". However, I was able to get it to work wrapping the label around the value:
<asp:Label runat="server"><%= DateTime.Now.ToString() %></asp:Label>
Set the Label1.Text = value instead of trying to use server side attrs inside of the server control
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 ''
Is it possible to get the value for StartingNodeUrl programmatically?
My sitemap has 3 levels to it at it's deepest and, depending on what section your are in I want to display all of the children below the parent in a navigation.
I reckon all I have to do is look at where I am in the navigation and put a value in StartingNodeUrl. But I cannot!
Code snippet from MasterPage:
<asp:Repeater ID="Repeater1" DataSourceID="SiteMapDataSource1" runat="server">
<ItemTemplate>
menu items here
</ItemTemplate>
</asp:Repeater>
<asp:SiteMapDataSource ID="SiteMapDataSource1" ShowStartingNode="false" StartingNodeUrl="/about/" runat="server" />
Thanks in advance.
JRenney
To get the StartingNodeUrl programmatically:
string startNodeUrl = SiteMapDataSource1.StartingNodeUrl;
It sounds like you're looking to operate from the current node though, so I imagine that the SiteMapDataSource.StartFromCurrentNode property is more useful for you.