日期:2014-05-17  浏览次数:20394 次

如何修改能运行通过?
后台有:
Image1.ImageUrl = "getimage.ashx?xingming=" + Session["name"] + "&isThumb=0";
前台有:
<asp:Image ID="Image1" runat="server" Width="200" Height="200" />
这两行能互相配合,成功显示出图片。

问:以下代码如何改才能正确显示出图片?(注:<%# Eval("教师姓名")%>不要改,它能正确显示数据)
<asp:Image ID="Image1" runat="server" Width="200" Height="200" ImageUrl='getimage.ashx?xingming=" + <%# Eval("教师姓名")%> + "&isThumb=0' />

附getimage.ashx代码如下:
C# code

//此处略去using
namespace prjSRI01
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class getimage : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            // Is id empty?
//            if(!String.IsNullOrEmpty(context.Request.QueryString["Id"]))
            if (!String.IsNullOrEmpty(context.Request.QueryString["xingming"]))
            {
                 // No, retrieve the image from the database.
                SqlConnection sqlCon = new SqlConnection();
                SqlCommand    sqlCmd = new SqlCommand();
                SqlDataReader sqlRdr;

                string imageType;
                
                if(context.Request.QueryString["isThumb"].Equals("0"))
                    imageType = "Thumbnail"; // get thumbnail from the database.
                else
                    imageType = "Image";     // get original image from the database.

                sqlCon.ConnectionString = 
                     ConfigurationManager.ConnectionStrings["dbSampleConnectionString"].ConnectionString.ToString();

                //sqlCon.ConnectionString = @"Data Source=ARISJB\SQLEXPRESS;" +
                //                           "Initial Catalog=dbSample;" +
                //                           "Integrated Security=True";

                if(sqlCon.State.Equals(ConnectionState.Closed))
                    sqlCon.Open();
                
//                sqlCmd.CommandText = String.Concat("SELECT ", imageType, " FROM tblSRI WHERE ID=@Id");
              sqlCmd.CommandText = String.Concat("SELECT ", imageType, " FROM tblSRI WHERE Username=@xm");

                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.Connection  = sqlCon;
//                sqlCmd.Parameters.AddWithValue("@Id", context.Request.QueryString["Id"]);
              sqlCmd.Parameters.AddWithValue("@xm", context.Request.QueryString["xingming"]);
                // Run the query.
                sqlRdr = sqlCmd.ExecuteReader(); 
                // Is record found?
                if(sqlRdr.HasRows)
                    // Yes, read the first record.
                    if(sqlRdr.Read())
                        // Return the image in binary format.
                        if (!sqlRdr[imageType].Equals(System.DBNull.Value ))
                        context.Response.BinaryWrite((byte[])sqlRdr[imageType]);

                sqlRdr.Close();    
            
                if(sqlCon.State.Equals(ConnectionState.Open))
                    sqlCon.Close();
            }
        }

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



------解决方案--------------------
<asp:Image ID="Image1" runat="server" Width="200" Height="