Click here to Skip to main content
16,016,712 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
hello everyone,

      I want to show image in a website, in which image should be selected dynamically from the database.....through a javascript function.....i am using <img> tag inside <item template> of a datalist......i had bind datalist through datasource......following isa the codea-behind for datalist......



<pre lang="cs">con.Open();
        cmd = new OleDbCommand(&quot;Select ID, PrjctName, Description, Facility, Status from Project&quot;, con);
        da = new OleDbDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        List_cmpltd.DataSource = ds;
        List_cmpltd.DataBind();
        con.Close();</pre>


this code is working properly.....
& following is the code for datalist in .aspx page.......


<pre lang="xml">&lt;asp:DataList runat=&quot;server&quot; ID=&quot;List_cmpltd&quot;&gt;
                    &lt;ItemTemplate&gt;
 &lt;td style="text-decoration: blink" valign="top"&gt;

                    &lt;div onload="fun('&lt;%# Eval("Status")%&gt;')"&gt;
                    &lt;img src='' id="img1" name="img1" alt="" width="100" height="50" /&gt;
                    &lt;/a&gt;
                   &lt;/div&gt;

            <pre>        &lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;</pre>


<pre lang="xml">&lt;/tr&gt;
                    &lt;/table&gt;
                    &lt;/ItemTemplate&gt;
                    &lt;SeparatorTemplate&gt;&lt;/SeparatorTemplate&gt;
                    &lt;/asp:DataList&gt;</pre>



& following isa the javasacript function i hv used for that......



<pre>&lt;script type="text/javascript"&gt;
    &lt;!--
    function fun(str)
    {
    if(str=="Completed")
    {
    img1.src= 'images/cmpltd.gif';
    }
    else if(str=="Current")
    {
    img1.src= 'images/crnt.gif';
    }
    else if(str=="Upcoming")
    {
    img1.src= 'images/upcmng.gif';
    }
    else
    {
    img1.src= 'images/plan.gif';
    }
    //--&gt;
    &lt;/script&gt;</pre>
/
Posted
Updated 15-Nov-11 16:20pm
v2

As Mr.Zaibshah said, How can you get the object if Image directly, You have to get Element of Image And set The Source Address..
JavaScript
var img=document.getElementById("img1")

And while setting image Src, How you get The Address of image, And where Does the Images folder located(if it is within your project then "/images/image1.jpg").
 
Share this answer
 
you can not use .net objects or properties in javascript unless you declare them globally.
in your case, you are accessing 'img1' directly, the javascript doesn't know that what it is.
you need to get img1 in var object to use it. i.e

JavaScript
var img = document.getElementById("img1");
img.src = "imagePath";


Try it this way
 
Share this answer
 
Comments
ujjwal uniyal 16-Nov-11 3:12am    
still not working...it is not showing any image.....
i have changed the java script code to the following......



<script type="text/javascript">
<!--
function fun(str)
{
var img=document.getElementById("img1");
if(str=="Completed")
{
img.src= 'images/cmpltd.gif';
}
else if(str=="Current")
{
img.src= 'images/crnt.gif';
}
else if(str=="Upcoming")
{
img.src= 'images/upcmng.gif';
}
else
{
img.src= 'images/plan.gif';
}
//-->
</script>

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