【问题标题】:ListView Rendering image issue. [duplicate]ListView 渲染图像问题。 [复制]
【发布时间】:2011-07-29 20:03:02
【问题描述】:

可能重复:
Rendering bytes from sql server to an image control?

<asp:ListView ID="lvGallery" runat="server" DataSourceID="SqlDataSource1">
   <LayoutTemplate>
   <table runat="server" id="tableGallery">
   <tr runat="server" id="itemPlaceHolder">

   </tr>

   </table>

   </LayoutTemplate>
   <ItemTemplate>       
   <tr><td>
   <div class="box_img2">
            <div class="g_size">
                <a runat="server" id="linkImage"><img id="Image1" runat="server" src="MyImage.jpg" /></a>
            </div>         
   </div>
   </td></tr>
   </ItemTemplate>
   </asp:ListView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="Data Source=CHANDAN-PC;Initial Catalog=Metamorphism;User ID=sa;password=chandan;" 
        ProviderName="System.Data.SqlClient" 
        SelectCommand="SELECT [Image] FROM [GalleryImages]"></asp:SqlDataSource>


public void ProcessRequest(HttpContext context)
        {
            //write your handler implementation here.
            string username = Convert.ToString(context.Request.QueryString["username"]);
            if (username != null)
            {
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter();
                byte[] arrContent;
                DataRow dr;
                string strSql;
                strSql = "Select Image from GalleryImages where username = '" + username + "'";
                da = new SqlDataAdapter(strSql, connection.ConnectionString);
                da.Fill(ds);
                dr = ds.Tables[0].Rows[0];
                arrContent = (byte[])dr["ImageFile"];
                context.Response.ContentType = "jpeg";
                context.Response.OutputStream.Write(arrContent, 0, arrContent.Length);
                context.Response.End();
            }
        }

我在 web.config 中添加了 httphandlers 标签。

【问题讨论】:

  • 同一问题不要发布超过两次
  • 将内容类型初始化字符串固定为:context.Response.ContentType = "image/jpeg";

标签: asp.net


【解决方案1】:

我认为你需要改变这个

     <div class="box_img2">
                <div class="g_size">
                    <a runat="server" id="linkImage">
<img id="Image1" runat="server" src='<%# Eval( HttpContext.Current.Request.QueryString["username"] ,"YourGenericHandler.ashx?username={0}") %>' /></a>
                </div>         
       </div>

【讨论】:

  • 如何将用户名作为参数与 img 控件的源属性一起传递?
  • 我更新了代码,但你不能为处理程序传递图像对象,你必须传递一个 id 左右,处理程序必须从数据库中获取数据并返回图像。
  • 它在运行时给我一个解析器错误....我认为语法不正确
  • 我收到以下错误。异常详细信息:System.ArgumentNullException:值不能为空。它没有获取查询字符串参数
  • 我会让它变得简单......我如何通过我的处理程序传递会话参数。例如:src='HttpImageHandler.aspx?username='
猜你喜欢
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 2023-02-01
相关资源
最近更新 更多