如何从aspx中的<ul>标签中定义的数据库中检索图像?</ul>

时间:2011-10-19 09:35:04

标签: asp.net sql-server-2005

我想要检索存在于数据库中的图像,图像标签iam在标签中给出,这就是为什么我没有得到我将如何做 这是我的aspx代码

 <div id="mask-gallery">
      <ul id="gallery" runat="server">
        <li>
          <img src="/images/projects/<%# Eval("Top_Image")%>" width="981" height="279" title="hello" alt="hello"/></li>
          <asp:Image ID="imgTop" ImageUrl='/images/Projects/<%# Eval("Top_Image") %>' runat="server" width="981" height="279" AlternateText="hello" />
       </ul>
    </div> 

这里是我的cs代码

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["Constr"]);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetImageData();
            }
        }
        protected void GetImageData()
        {
            SqlParameter[] param = new SqlParameter[1];

            param[0] = new SqlParameter("@Id_Project", SqlDbType.VarChar, 300);
            param[0].Value = lblid.Text.ToString().Trim();

            SqlCommand Cmdd = new SqlCommand("tpRetrieve_Project_Master_Top_Image", con);
            Cmdd.CommandType = CommandType.StoredProcedure;

            Cmdd.Parameters.Add(param[0]);
            con.Open();

            SqlDataReader drr = Cmdd.ExecuteReader();


            if (drr.HasRows)
            {
                while (drr.Read())
                {
                    hd_Show_In_Top.Value = drr["Show_In_Top"].ToString().Trim();
                    if (hd_Show_In_Top.Value == "Yes")
                    {
                        hd_thumbnail.Value = drr["Top_Image"].ToString().Trim();
                        lblbdescription.Text = drr["Description"].ToString().Trim();
                        lblbprojecttype.Text = drr["Project_type"].ToString().Trim();
                        // lblrotation.Text = lblrotation.Text + "<a href='/about-us/people.aspx'><img class='cloudcarousel'  src='/images/AboutUsTopImages/" + hd_thumbnail..Value.ToString().Trim() + " '/></a>";
                    }
                }
            }

            drr.Close();
            con.Close();
        }

3 个答案:

答案 0 :(得分:2)

一种可能的(实际上非​​常常见的)方法是创建一个HttpHandler,它将从数据库中获取图像并将其写入响应。查看this link以查看有效示例。

答案 1 :(得分:0)

如果您将图像名称与扩展名一起存储在数据库中,则可以执行此类操作

var x = datasource();//your datasource which contains the imagefile

<img src='" + Page.ResolveUrl("~/images/projects/") + x.imageFile + "' alt='Test' />

答案 2 :(得分:0)

如果Top_Image包含filename.ext(sample.jpeg),那么你应该试试这个,

<img runat="server"
     src='<%# Eval("Top_Image","~/images/projects/{0}") %>'
     width="981" 
     height="279" 
     title="hello" 
     alt="hello"/>
    </li>
<asp:Image ID="imgTop" 
        ImageUrl='<%# Eval("Top_Image","~/images/Projects/{0}") %>'
        runat="server" 
        width="981" 
        height="279" 
AlternateText="hello" />

如果您没有绑定DataSource,则无需使用Eval()方法。您可以在后面的代码中指定ImageUrl属性。

if (drr.Read())
  {
    hd_Show_In_Top.Value = drr["Show_In_Top"].ToString().Trim();
    if (hd_Show_In_Top.Value == "Yes")
    {
       imgTop.ImageUrl="~/images/Projects/" +drr["Top_Image"].ToString().Trim();
       ....
    }
  }