Click here to Skip to main content
15,394,700 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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" "">

<html xmlns="">
<head runat="server">
    <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()
            string qry = "select count(id) from Image";
            cmd = new SqlCommand(qry, cn);
            dr = cmd.ExecuteReader();
            while (dr.Read())
                j = (int)dr[0];
            return j;

        public string getImgUrl()
            cmd = new SqlCommand("select img_url from Image",cn);
            string qry = "select img_url from Image";
            adp = new SqlDataAdapter(qry, cn);
            ds = new DataSet();
            for (int i = 0; i < getCount(); i++)
                str = ds.Tables[0].Rows[i]["img_url"].ToString();
            return str;
    <form id="form1" runat="server">
   <%  int c = getCount(); %>
        <table cellpadding="0" cellspacing="0" style="width: 100%">
       <%for (int i = 0; i < c; i++){
       <%    for (int j = 0; j < 3; j++)
          { str = getImgUrl();
            <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>
    <% } %>
    <% } %>

plz help me..
thank u..........
Updated 15-Feb-14 16:40pm
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.

Member 9877729 15-Feb-14 22:42pm

1 solution

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 img tag.

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