Click here to Skip to main content
14,328,797 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi,
Can i add images dynamically from database in img tag without using any upload button,i want to load images in img tag on page_load, so how can i do..? suppose there are 3 images in database and they must be added on img tag respectively while page will load...
I have code like...

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="div.aspx.cs" Inherits="User_Images_div" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Styles/StyleSheet.css" rel="stylesheet" type="text/css" />

    <script runat="server">
        SqlConnection cn = new SqlConnection("Data Source=COMPUTER;Initial Catalog=demo_page;Integrated Security=True");
        SqlCommand cmd;
        SqlDataReader dr;
        DataSet ds;
        SqlDataAdapter adp;
        int j;
        static string str;
        public int getCount()
        {
            cn.Open();
            string qry = "select count(id) from Image";
            cmd = new SqlCommand(qry, cn);
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                j = (int)dr[0];
            }
            cn.Close();
            return j;
        }

        public string getImgUrl()
        {
            cn.Open();
            cmd = new SqlCommand("select img_url from Image",cn);
            string qry = "select img_url from Image";
            adp = new SqlDataAdapter(qry, cn);
            ds = new DataSet();
            adp.Fill(ds);
            for (int i = 0; i < getCount(); i++)
            {
                str = ds.Tables[0].Rows[i]["img_url"].ToString();
            }
           
            cn.Close();
            return str;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
   <%  int c = getCount(); %>
        <table cellpadding="0" cellspacing="0" style="width: 100%">
       <%for (int i = 0; i < c; i++){
       %>
       <tr>
       <%    for (int j = 0; j < 3; j++)
          { str = getImgUrl();
       %>
           <td>
            <div class="prod_box">
                    <div class="top_prod_box"></div>
                    <div class="center_prod_box">
                    <div class="product_title"></div>
                    <div class="product_img">
                    <asp:Image ID="Image1" ImageUrl='<%=str %>' Width="146px" Height="73px" runat="server" />
                    <%--<img src=<%#str%> alt="" />--%></div></div>
                    <div class="bottom_prod_box"></div>
                    <div class="prod_details_tab" align="center"></div>
        </div>
        </td>
    <% } %>
    </tr>
    <% } %>
    </table>
    </form>
</body>
</html>


plz help me..
thank u..........
Posted
Updated 15-Feb-14 16:40pm
v2
Comments
Nelek 15-Feb-14 13:56pm
   
What have you tried?[^]
How to ask a question?[^]

Just saying "I want to..." "how to..." is not going to bring you the results you expect. Please read those two links and remake your question if you want to get a better answer
ZurdoDev 15-Feb-14 14:23pm
   
What's your question?
Ravi Bhavnani 15-Feb-14 14:42pm
   
In addition to the other comments, I suggest you don't use txtspk.  Take the trouble to formulate your question properly if you expect the CodeProject community to take the trouble to help you.

/ravi
Member 9877729 15-Feb-14 22:42pm
   
sorry...

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You can make any usual HTML tag to have server functionality by adding into the tag runat="server" and giving it a name with id="MyName"

In this case I'd suggest you used a div with server functionality...


<div  runat="server" id="ImagesDiv"></div>


So then in code-behind, you can just have those images into a DataTable perhaps, "dtImages"

string htmlForImages = String.Empty;

for (int i = 0; i < dtImages.rows.Count; i++)
   htmlForImages += "<img src="" + dtImages.Rows[i][0].ToString() + "" />";

ImagesDiv.InnerHtml = htmlForImages;




This solution works for every case in which you can't tell exactly how many images there... there could be 1, 2, 3... 100 and it would still work.

Unless you had a fixed amount of images, then you could use asp.net img tag.
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100