通过从数据库中提取图像URL,动态添加图像路径到html中的img标签。? [英] Add image path dynamically to img tag in html by fetching image url from database..?

查看:69
本文介绍了通过从数据库中提取图像URL,动态添加图像路径到html中的img标签。?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以在img标签中动态添加图片而不使用任何上传按钮,我想在page_load上的img标签中加载图片,那我该怎么办...?假设数据库中有3个图像,它们必须分别添加到img标签上,而页面将加载...

我的代码类似......




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

推荐答案

您可以通过添加到标签runat =server并为其命名来使任何常用的HTML标记具有服务器功能id =MyName



在这种情况下,我建议你使用带服务器功能的div ...





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>





那么在代码隐藏中,你可以把这些图像放到DataTable中,dtImages





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;









此解决方案适用于您无法确切知道有多少图像的每种情况......可能有1,2,3 ... 100它仍然可以工作。



除非你有固定数量的图像,否则你可以使用asp.net img标签。





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.


这篇关于通过从数据库中提取图像URL,动态添加图像路径到html中的img标签。?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆