C#ASP.net使用HTTPHandler从列表中将图像绑定到GridView

时间:2015-11-05 02:59:33

标签: c# asp.net httphandler

我目前有一个gridview,应使用自定义HTTPHandler填充多个图像预览。我已经有了物品清单。问题是,每当我尝试实现绑定时,所有绑定的图像都来自项目列表的最后一个条目而忽略其他项目。我想知道为什么会这样?

MyHandler.ashx:

List<string> items = new List<string>();
items = ItemManager.GetImages();


 public void ProcessRequest (HttpContext context) {
        WebClient client = new WebClient();
        if (items.Count > 0)
        {         
            foreach (var a in items)
            {
                byte[] buff = client.DownloadData(a);
                context.Response.ContentType = "image/jpeg";
                context.Response.AddHeader("content-length", buff.Length.ToString());
                context.Response.BinaryWrite(buff);
            }              
        }      
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

Gridview.aspx

<asp:GridView ID="gv1" style="text-align:center;" PageSize="6" Width="100%" AutoGenerateColumns="false" runat="server" BackColor="White" BorderColor="Black" BorderStyle="None" BorderWidth="1px" CellPadding="10"
                         AllowPaging="true">
                            <Columns>

                                <asp:ButtonField ButtonType="Button" CommandName="Select" ControlStyle-Width="100" ControlStyle-Height="30" Text="Update" HeaderText="Update"/>
                                <asp:ButtonField ButtonType="Button" CommandName="Delete" ControlStyle-Height="30" Text="Delete" HeaderText="Delete"/>


                                <asp:BoundField DataField="a" HeaderText="ID" />
                                <asp:BoundField DataField="b" HeaderText="Title" />
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <img src="MyHandler.ashx" class="gridview-imgPreview" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="c" HeaderText="Caption" />
                                <asp:BoundField DataField="d" HeaderText="Description" />

                            </Columns>

0 个答案:

没有答案