Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've bound records in datalist using JQuery, There is no problem displaying the record inside the datalist. But while i'm trying to dynamically bind image inside the datalist i can't.
I've tried 2 way to bind image inside datalist.
1. Image stored as image datatype and tried to bind in the datalist
2. save image in the web server, save the image path in the database and tried to bind image inside datalist.
Both way i've failed to bind image.
Here is my code snippet through which i've bind the datalist

in aspx page
C#
<script type="text/javascript">
        $(function () {
            $("[id*=dlCustomers]").hide();
            $.ajax({
                type: "POST",
                url: "index.aspx/GetCustomers",
                data: '{}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response.d);
                },
                error: function (response) {
                    alert(response.d);
                }
            });
        });

        function OnSuccess(response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Table");
            var repeatColumns = parseInt("<%=dlCustomers.RepeatColumns == 0 ? 1 : dlCustomers.RepeatColumns %>");
            var rowCount = Math.ceil(customers.length / repeatColumns);
            var i = 0;
            while (i < repeatColumns * rowCount) {
                var row = $("[id*=dlCustomers] tr").eq(0).clone(true);
                for (var j = 0; j < repeatColumns; j++) {
                    var customer = $(customers[i]);
                    if (customer.length == 0) {
                        $("table:last", row).remove();
                    } else {
                        $(".proid", row).eq(j).html(customer.find("Proid").text());
                        $(".product", row).eq(j).html(customer.find("Product").text());
                        $(".details", row).eq(j).html(customer.find("Details").text());
                        $(".imgpath", row).eq(j).html(customer.find("imgpath").text());
                    }
                    i++;
                }
                $("[id*=dlCustomers]").append(row);
            }
            $("[id*=dlCustomers] tr").eq(0).remove();
            $("[id*=dlCustomers]").show();
        }
    </script>

here is the datalist
C#
<asp:DataList ID="dlCustomers" runat="server" RepeatLayout="Table" RepeatColumns="3"
    CellPadding="0" CellSpacing="0">
    <ItemTemplate>
<table class="box">
                                <tr>
                                    <td>
<img src=<%# Eval("imgpath")%>"  width="150" alt="" />
                                    <h3><u><span class="companyname"><%# Eval("CompanyName")%></span></u></h3>
                                    <p><b><u><span class="product"><%# Eval("Product")%></span></u></b></p>
</td>
                                </tr>
        </table>
    </ItemTemplate>
</asp:DataList>


Finally cs page
C#
[WebMethod]
    public static string GetCustomers()
    {
        string query = "SELECT * FROM tbl_Product where Featured='True'";
        SqlCommand cmd = new SqlCommand(query);
        return GetData(cmd).GetXml();
    }

    private static DataSet GetData(SqlCommand cmd)
    {
        string strConnString = ConfigurationManager.ConnectionStrings["DynamicConnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    return ds;
                }
            }
        }
    }



Hope this above code helps to understand my logic; Please help me to solve this. also tell me which will be the best way to store image (image datatype or image path in the database).

Thanks in advance.
Posted
Updated 20-Jun-13 21:59pm
v6
Comments
Madhur Kapoor 21-Jun-13 3:42am    
After your data binding bindings happens, can you check what is the html that is generated for the <img> tag.
sahabiswarup 21-Jun-13 3:50am    
yes the tag generated properly, but the image path which is saved on the database is not reflecting there.

1 solution

<title>


$(function () {
$("[id*=dlCustomers]").hide();
$.ajax({
type: "POST",
url: "CS.aspx/GetCustomers",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
});

function OnSuccess(response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("Table");
var repeatColumns = parseInt("&lt;%=dlCustomers.RepeatColumns == 0 ? 1 : dlCustomers.RepeatColumns %&gt;");
var rowCount = Math.ceil(customers.length / repeatColumns);
var i = 0;
while (i &lt; repeatColumns * rowCount) {
var row = $("[id*=dlCustomers] tr").eq(0).clone(true);
for (var j = 0; j &lt; repeatColumns; j++) {
var customer = $(customers[i]);
if (customer.length == 0) {
$("table:last", row).remove();
} else {
$(".name", row).eq(j).html(customer.find("ContactName").text());
$(".city", row).eq(j).html(customer.find("City").text());
$(".postal", row).eq(j).html(customer.find("PostalCode").text());
$(".country", row).eq(j).html(customer.find("Country").text());
$(".phone", row).eq(j).html(customer.find("Phone").text());
$(".fax", row).eq(j).html(customer.find("Fax").text());
$(".image", row).eq(j).attr("src", customer.find("Image").text());
}
i++;
}
$("[id*=dlCustomers]").append(row);
}
$("[id*=dlCustomers] tr").eq(0).remove();
$("[id*=dlCustomers]").show();
}




<asp:datalist id="dlCustomers" runat="server" repeatlayout="Table" repeatcolumns="3" xmlns:asp="#unknown">
CellPadding="2" CellSpacing="2">
<itemtemplate>


<%# Eval("ContactName") %>

City: ]]>

Postal Code: ]]>

Country: ]]>

Phone: ]]>

Fax: ]]>










refer link http://aspforums.net/Threads/566882/Populate-ASPNet-DataList-with-Image-by-binding-DataSet-Client-Side-using-jQuery-AJAX/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900