I am retrieving an XML response from an Ajax call and I'm trying to set the image on my datalist from the image name which comes from the XML. This appears to be problematic. The way I am trying to set the image is like this:
$(".Image", tablex).attr('src', product.find("Image").attr('src'));
However, this doesn't seem to work right because on the rendered results I see the image from the first item on the first row being replicated/rendered on each of my newly added items. Even if I set the attribute like below:
$(".Image", tablex).attr('123456');
the results are the same as above, so I believe my code within the attr has no effect!
If I set the code like this:
$(".Image", tablex).html(product.find("Image").text()); then I can see the correct file name being rendered on the page.
Here is my function:
function OnSuccess(response) {
var xmlDoc = $.parseXML(response);
var xml = $(xmlDoc);
pageCount = parseInt(xml.find("PageCount").eq(0).find("PageCount").text());
var customers = xml.find("Customers");
customers.each(function () {
var product = $(this);
var tablex = $("#dvProducts div").eq(0).clone(true);
$(".Image", tablex).attr('src', product.find("Image").attr('src'));// <- Problem is here!
$(".ProductID", tablex).html(product.find("ProductID").text());
$(".Name", tablex).html(product.find("Name").text());
$("#dvProducts").append(tablex);
});
}
My datalist
<div id="dvProducts">
<asp:DataList ID="rptCustomers" runat="server" BorderColor="Black" CellPadding="0" CellSpacing="0" RepeatLayout="Table" RepeatDirection="Horizontal" >
<ItemTemplate>
<div class="wrapping">
<div id="boxer">
<span class="Image"><asp:Image ID="Image" runat="server" CssClass="topimage" ImageUrl='<%# "~/images/topimages/" &Eval("Image")%>' /></span>
<br />
<span class="ProductID"><asp:Label ID="ProductID" runat="server" Text='<%# Eval("ProductID") %>' /></span>
<br />
<span class="Name"><asp:Label ID="Name" runat="server" Text='<%# Eval("Name")%>' /></span>
</div>
</div>
<br />
</ItemTemplate>
</asp:DataList>
</div>
XML response with the data for the new three items.
"<NewDataSet>
<Customers>
<RowNumber>4</RowNumber>
<SubCatName>Teenagers Phones</SubCatName>
<ProductID>6</ProductID>
<SubCategoryID>2</SubCategoryID>
<Name>HTC b</Name>
<Description>thcs</Description>
<Image>htc3.jpg</Image>
</Customers>
<Customers>
<RowNumber>5</RowNumber>
<SubCatName>Teenagers Phones</SubCatName>
<ProductID>5</ProductID>
<SubCategoryID>2</SubCategoryID>
<Name>HTC a</Name>
<Description>htca</Description>
<Image>htc2.jpg</Image>
</Customers>
<Customers>
<RowNumber>6</RowNumber>
<SubCatName>Teenagers Phones</SubCatName>
<ProductID>4</ProductID>
<SubCategoryID>2</SubCategoryID>
<Name>HTC</Name>
<Description>htc</Description>
<Image>htc1.png</Image>
</Customers>
<PageCount>
<PageCount>4</PageCount>
</PageCount></NewDataSet> "
Maybe the relative path is interfering with something, or why does my attr code does not getting picked up and I get the same image on every new item? Any ideas?