listview中只出现一个图像

时间:2010-08-24 21:11:47

标签: asp.net

我有一个通用处理程序

public void ProcessRequest(HttpContext context)
    {

        context.Response.ContentType = "image/jpeg,png,jpg,gif";
        int newsId = int.Parse(context.Session["newsId"].ToString());
        int FK_UnitId = int.Parse(context.Session["UserData"].ToString());
        Managers.Photo p = new Managers.Photo();
        string dirPath = ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsImages" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId.ToString() + "/";
        string dirPathForTextFiles = ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsTextFiles" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";
        DataTable dt = p.GetAllPhotos(newsId);
        List<string> l = new List<string>(dt.Rows.Count);
        byte[] b = null;
        FileStream f;
        try
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                l.Add(dirPath + dt.Rows[i]["photoName"].ToString());
                f = new FileStream(l[i].ToString(), FileMode.Open, FileAccess.ReadWrite);
                b = new byte[f.Length];
                f.Read(b, 0, b.Length);
                context.Response.OutputStream.Write(b, 0, b.Length);
                context.ClearError();
                f = null;
                b = null;

            }
        }
        catch (IOException e)
        {
           string message =  e.Message;
        }


    }

和我页面上的列表视图:.cs文件包含

Session.Add("newsId", newsId);  
string dirPath =ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsImages" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";  
string dirPathForTextFiles =ConfigurationManager.AppSettings.GetValues("ThePath").First() + "/" + "NewsTextFiles" + "/" + "UnitNum" + FK_UnitId.ToString() + "_" + "NewsNum" + newsId + "/";  
DataTable dt = p.GetAllPhotos(int.Parse(newsId));  
List<string> l = new List<string>(dt.Rows.Count);  
for (int i = 0; i < dt.Rows.Count; i++)  
{  
    l.Add(dirPath + dt.Rows[i]["photoName"].ToString());  
}  
lv_showImages.DataSource = l;  
lv_showImages.DataBind();

我的来源

<asp:ListView ID="lv_showImages" GroupItemCount="4" runat="server">
    <ItemTemplate>
         <asp:Image ID="img_newsImage" Height="100px" Width="100px" runat="server ImageUrl ='<%# "RetreiveImage.ashx" %>' />
   </ItemTemplate>  
</asp:ListView>`

现在我的问题:

当列表有多个图像集路径时,只有第一个图像出现并重复,虽然我调试了我的处理程序,我发现字节数组的b长度在循环中有所不同,这意味着它是应该写不同的图像,而不是按照图像的数量重复第一个图像,我不知道为什么第一个图像只出现。

3 个答案:

答案 0 :(得分:0)

我认为您不能在一个请求中返回多个图像。我猜你正在将所有图像的内容写入响应,但浏览器只读到第一张图像的末尾。您必须更改处理程序以接受图像标识符参数并仅返回该图像。

答案 1 :(得分:0)

您是否错误地复制了代码或忘记关闭aspx页面中的代码?如果它没有关闭(和编译),那可能是你没有看到多个图像的原因。

答案 2 :(得分:0)

我遇到了类似的问题。

认为可能是您正在使用List<string>而不是List<T>,其中包含一个包含字符串属性的对象:string imagePath { get; set; }

由于某种原因,DataBind无法迭代字符串声明,只能迭代对象属性。