在单独的页面上显示图像

时间:2011-07-22 22:27:28

标签: asp.net

我有一个图像数据类型,点击链接按钮需要在单独的页面上显示

aspx page
    detail view: 
    <asp:TemplateField HeaderText="Evidence (if any)">
                  <ItemTemplate>
                    <asp:LinkButton ID="lbEvidence" runat="server"
                     Text='<%# DataBinder.Eval(Container.DataItem, "Evidence").ToString()    == String.Empty ? "None" : DataBinder.Eval(Container.DataItem, "Evidence")%>'
                            CommandName="Select" CommandArgument = '<%# DataBinder.Eval(Container.DataItem, "Complaint_Id") %>'> </asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>

ImageHandler.ashx

    <%@ WebHandler Language="C#" Class="FMMadminModule.imageHandler" %>

using System;
using System.Web;
using System.Data;
using System.Web.SessionState;

    namespace FMMadminModule
    {
    public class imageHandler : IHttpHandler, IReadOnlySessionState
    {
    DataTable dt;
    int key;
    byte[] imageOut;
    public void ProcessRequest(HttpContext context)
    {            
        HttpResponse response = context.Response;
        HttpRequest request = context.Request;
        context.Response.ContentType = "image/jpeg";
        response.BufferOutput = false;
        // get the key, the index into the DataTable
        key = Convert.ToInt32(request.QueryString["Complaint_ID"]);           
        // Prepare the datatable to hold the SNo key and the jpeg image, which will be written out 
        dt = new DataTable();
        dt = (DataTable)context.Session["dt"];
        if (!dt.Rows[key]["Evidence"].Equals(null))
        {
            imageOut = (byte[])dt.Rows[key]["Evidence"];
            response.OutputStream.Write(imageOut, 0, imageOut.Length);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
}

如何在单独的页面上显示图像? 这就是aspx.cs的样子

 protected void dvResolveComplaint_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            DetailsViewRow row = dvResolveComplaint.Rows[5];
            //String RowId = (e.NewSelectedIndex).ToString(); 
            Type csType = this.GetType();
            String strScript = "<script> ";
            strScript += @"var newWindow = window.open('imageHandler.ashx?Complaint_ID=" + e.CommandArgument + @"', 'Evidence', ' height=450, center:yes, width=600, status=no, resizable= yes, menubar=no, toolbar=no, location=yes, scrollbars=no, status=no')";
            strScript += "</script>";
            ClientScript.RegisterClientScriptBlock(csType, "ViewEvidence", strScript);
            dvResolveComplaint.Attributes.Add("OnClick", strScript);
        }
    }

1 个答案:

答案 0 :(得分:1)

这样做过于复杂。你根本不需要链接按钮,只需在你的项目模板中写一个锚包裹的图像并在一天内调用它,例如:

<a href='url_to_your_handler' target="_blank"><img src='url_to_your_handler' alt='complaint image' /></a>