PDA

View Full Version : مبتدی: استفاده ازیک Generic Handler در یک صفحه برای چندتا Image



hadinajafi
چهارشنبه 07 مرداد 1394, 21:17 عصر
سلام کد من بصورت زیر

<asp:Image ID="Image" runat="server"
ImageUrl='<%#HandlerPhoto.ashx?id=" + Eval("id")+"&index=1" %>'
Width="158px" BorderStyle="Solid" BorderWidth="2px"
BorderColor="#000066" AlternateText=" گواهی مهارت " />

asp:Image ID="Image1" runat="server"
ImageUrl='<%# "HandlerPhoto.ashx.ashx?id=" + Eval("id")+"&index=2" %>'
Width="158px" BorderStyle="Solid" BorderWidth="2px"
BorderColor="#000066" AlternateText=" تصویر شنامه " />
انم کد HandlerPhoto.ashx

public string GetConnectionString()
{
//sets the connection string from your web config file "ConnString" is the name of your Connection String
return System.Configuration.ConfigurationManager.Connecti onStrings["portalConnectionString"].ConnectionString;
}

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpg";
string id = context.Request.QueryString["id"];
string index = context.Request.QueryString["index"];
if (id != null)
{

MemoryStream memoryStream = new MemoryStream();
SqlConnection connection = new SqlConnection(GetConnectionString());
string sql = "SELECT govahi_maharat, tasvir_shenasname, tasvir_kartMeli, tasvir_payan_khedmat, tasvire_madrak, tasvir_mahal_masul ,form_tahod FROM tbl_person WHERE [id] = @id";

SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@id", id);
connection.Open();

SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
byte[] file=null;
switch (index)
{
case "1":
if (!(reader["govahi_maharat"] is DBNull))
{
file = (byte[])reader["govahi_maharat"];
}



break;
case "2": file = (byte[])reader["tasvir_shenasname"];
break;
case "3": file = (byte[])reader["tasvir_kartMeli"];
break;
case "4": file = (byte[])reader["tasvir_payan_khedmat"];
break;
case "5": file = (byte[])reader["tasvire_madrak"];
break;
case "6": file = (byte[])reader["tasvir_mahal_masul"];
break;
case "7": file = (byte[])reader["form_tahod"];
break;


}
// Get Image Data
reader.Close();
connection.Close();
if (file!=null)
{
memoryStream.Write(file, 0, file.Length);
context.Response.Buffer = true;
context.Response.BinaryWrite(file);
}

memoryStream.Dispose();
}

}

public bool IsReusable
{
get
{
return false;
}
}

pbm_soy
پنج شنبه 08 مرداد 1394, 02:06 صبح
خوب مشکل چیست ؟!