I'm trying to list vacancies in a asp:GridView using an XML_feed through an asp:XmlDataSource. This should be kinda simple, but I'm missing something with my XPath-expressions..
This is a short example of what I'm trying to achieve (list title of vacancies):
<asp:XmlDataSource ID="XMLsource" DataFile="http://demo.easycruit.com/export/xml/vacancy/list.xml" XPath="VacancyList/Vacancy" runat="server"/>
<asp:GridView DataSourceID="XMLsource" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>Title</HeaderTemplate>
<ItemTemplate><%# XPath( "Version/Title" ) %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Both controllers have DataBind() in codebehind. So, if someone knows why this doen't work... :)
If you have a look at your XML (just the first few lines):
<VacancyList generated="2009-08-04T18:43:17"
xmlns="urn:EasyCruit"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.easycruit.com/dtd/vacancy-list.xsd">
<Vacancy id="82034" date_start="2007-04-17" date_end="2009-12-22"
reference_number="CDP-GR3">
<Versions>
<Version language="fr">
<Title>Chef de produit (H/F)</Title>
<TitleHeading/>
You bind your datagrid to the list of <Vacancy> elements - fine so far.
But then in your grid, you reference : <%# XPath( "Version/Title" ) %>
This won't work, since the <Vacancy> does not have a '/inside it - these elements are within a` collection.....
So what you need to reference in your ItemTemplate would be:
<%# XPath( "Versions/Version[#language='fr']/Title" ) %>
That should work.
UPDATE:
there appears to be an additional problem with the ASP.NET 2.0 XmlDataSource not being able to handle default XML namespaces :-(
That's this line here in your XML:
<VacancyList ........
xmlns="urn:EasyCruit"
See this blog post here on the topic: http://jasonf-blog.blogspot.com/2006/08/xmldatasource-xpath-workaround-for.html
There's really two things you could do to fix this:
use an XSLT transformation to strip out the default namespace so that the XmlDataSource can handle the data
load the data from the URL in code, and bind it to the GridView in your code-behind
UPDATE 2:
The method of stripping out the XML namespaces seems to work quite nicely - Bill Evjen proposes this method here.
If you save his XSLT file in the post to a file called "StripNamespaces.xslt" in the web site project, you should get your data if you change the asp:XmlDataSource to be:
<asp:XmlDataSource ID="XMLsource" runat="server"
DataFile="http://demo.easycruit.com/export/xml/vacancy/list.xml"
TransformFile="~/StripNamespaces.xslt"
XPath="VacancyList/Vacancy" />
Note the new "TransformFile" setting - this must reference that XSLT file. With this in place, I am now getting data displayed in the GridView.
Marc
Related
Output
Code
<asp:Repeater ID="rptPBM" runat="server">
<ItemTemplate>
<asp:Label runat="server" ID="lblPBM" EnableViewState="false" Text='<%# Container.DataItem.ToString() %>'/>
<br />
</ItemTemplate>
</asp:Repeater>
I'm trying to add labels to a page dynamically using the above repeater, but when they are added it stacks vertically. Anyone ever encounter this before?
You are adding a line break after each label (br tag). Was that intended?
Remember that for each item in your data set it will write out that whole statement between the tags.
Maybe try writing out the html manually until you get it how you like and then put it into the item template.
Found the problem, the DataSource and DataBind() were in a loop so it was adding one letter at a time. Once I moved them out, it works fine.
I'm just learning ASP.Net, so I hope that you bear with me and my questions. In my program, I have a dataset that contains Url strings that point to various images. My question is, can I use that dataset as a datasource for a Repeater control so that the Repeater uses those Urls to display the images?
Thanks so much for any help and advice.
You most certainly can. You will want to do the binding in the code-behind, probably in the Load method like this:
repeaterControl.DataSource = yourDataSet.Tables[0];
repeaterControl.DataBind();
where 0 is the index of the DataTable you're trying to get to.
Then you'll want to build the markup something like this:
<asp:Repeater ID="repeaterControl" runat="server">
<ItemTemplate>
<asp:Image runat="server"
ImageUrl="<%# DataBinder.Eval(Container.DataItem, "TheFieldName") %>" />
</ItemTemplate>
</asp:Repeater>
where TheFieldName is the name of the field/column in the DataTable that contains the URL. Now, this code may need to be debugged a little bit because I didn't build an entire project around this, but this will get you 99% of the way there, if not all of the way.
this is definitely an easy question but I still don't know what exactly it is for. Can anybody tell me what ImageUrl='<%# Eval("FileName") %>' means? I still don't get the idea why we need to include %#.
<%# Eval("FileName") %> is used in the context of binding data from a collection to a control. Probably the value for the imageurl is coming from a property of an object in the collection
For example, List<Photo> where Photo has a property of FileName. If you are binding that to a gridview, a repeater, etc, you'll access that property for each item in the collection when binding to such controls
Asp.net data binding overview
in this Line...
ImageUrl='<%# Eval("FileName") %>'
ImageURL the attribute of your asp:ImageButton control that is used to specify the Url of the Image File to be Used
Code between '<% and %>' tags are writtent to be Executed on the Server
'#' is used to specify that the result of server side execution will be bound hear
Eval KeyWord is Used To Evaluate the perticular Column Value (that you specify ("--hear--")) from The DataSourse
When you are using a Template Control like Repeater , GridView , etc. you are actually iterating in a list of data records, and <%# Eval("FileName") %> here means give me the value of the column named FileName.
Here we have used the Eval function which is used for one way databinding. FileName is the field name you are associating. Anything that's written inside <%# %> is parsed by asp.net engine before generating the webpage source which is pure client side script and html tags.
So Eval function is executed at server end by ASP.net engine.
I have GridView with my DAO as data source rendering some person data (name, surname...). I want to add to rendered table simple link to expanded view with more informations. Ithought I could add html link with POST argument to each row. But every html I try to pass to GridView gets escaped. Can I somehow unescape it? Or is there any other simple way?
It is my private, very quick project, I donĀ“t need any robust solution. Just the simplest and quickest one. Thanks.
It sounds like you want to use an ItemTemplate in your gridview to render HTML controls within the grid:
asp:GridView ID="myGrid" runat="server">
<Columns>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<!--Any HTML can go here, below is a label -->
<label><%# DataBinder.Eval (Container.DataItem, "lastname") %><label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Can somebody please explain this to me:
I have a label and I want to be able to set the Text property by calling a method in the aspx file. It works fine if I set the property in code behind, but I really want to set this property in the aspx file.
I have tried a couple of things, but what I expected to work was this:
<asp:Label ID="Label1" runat="server" Text=<%# GetMyText("LabelText") %> />
I get no errors when doing this, but my method is never called and the Text property is left empty.
Is it not possible to set property values to server side controls directly in the aspx without using resources or use hard coded values?
Update: My first try was:
<asp:Label ID="Label1" runat="server" Text=<%= GetMyText("LabelText") %> />
But that results in the following error:
Server tags cannot contain <% ... %> constructs.
The syntax =<%# ... %> is Data binding syntax used to bind values to control properties when the DataBind method is called.
You need to call DataBind - either Page.DataBind to bind all the controls on your page, or Label1.DataBind() to bind just the label. E.g. add the following to your Page_Load event handler:
if (!IsPostBack)
{
this.DataBind();
// ... or Label1.DataBind() if you only want to databind the label
}
Using Text='<%= GetMyText("LabelText") %>' as others have proposed won't work as you'll find out. This syntax is inherited from classic ASP. It can be used in some circumstances in ASP.NET for inserting dynamic values in static HTML, but can not be used for setting propeties of server controls.
The sysntax you are looking for is <%= %> the # is for data binding. So your code should read:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
EDIT: This answere is incrrect
I am leaving this answer here because lots of people agreed with me that this is infact the correct answer, but it will not work. This line of code will produce the following HTML output:
<span id="Label1"><%= GetMyText("LabelText") %></span>
Try this:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
Edit
Yep. I was wrong. #Joe was right.
However, THIS works (and I'm not sure what the difference is):
<asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>
CodeBehind:
protected string GetMyText(string input)
{
return "Hello " + HttpUtility.HtmlEncode(input);
}