2010-08-24 30 views
0

我有一个通用的处理程序只有一个形象出现在列表视图

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的长度在循环中有所不同,但这意味着它应该写入不同的图像,而不是重复第一张图片的图片数量,我不知道为什么第一张图片才出现。

+0

您是否使用过? – Alex 2010-08-25 13:13:10

回答

0

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

+0

但我想返回多个图像,所以我可以删除,管理我想要的, 当我调试处理程序我敢肯定,大小的数组每次都不相同,所以我得出结论,它写所有的图像,但我不'不知道为什么只有第一个图像出现,它出现了几次(图像的数量) – 2010-08-24 22:09:12

+0

我做了你的建议,我使用分页来移动图像,它的工作原理,非常感谢你。 – 2010-08-25 18:00:23

0

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

+0

对不起,我复制我的代码错误我现在编辑它,谢谢 – 2010-08-25 06:11:41

0

我有一个相当类似的问题。

认为它可能是你使用的是List<string>,而不是其中包含持有字符串属性的对象的List<T>的:string imagePath { get; set; }

出于某种原因,DataBind无法遍历字符串声明并只遍历对象属性。

+0

根本没有图像。 – 2010-08-25 07:47:50

+0

在我的项目中,我完全抛弃了List,并将单个字符串属性用于最多4个元素。我不认为嵌套列表的ASP很好。对不起,它没有工作:( – Alex 2010-08-25 10:30:25