2016-08-23 3 views
-1

Я не могу показать изображения из базы данных в gridview. Я могу их спасти. Когда я пытаюсь получить его, он не отображает ничего, кроме имени продукта.Невозможно отобразить изображение в GridView

Вот мой GridView.

<Columns> 
     <asp:CommandField ShowDeleteButton="True" /> 
     <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /> 
     <asp:ImageField DataImageUrlField = "ProductImagesID" 
      DataImageUrlFormatString = "../ImageHandler.ashx?ProductImagesID={0}" 
      ControlStyle-Width = "75" ControlStyle-Height = "75" 
      HeaderText = "Preview Image"> 
       <ControlStyle Height="100px" Width="100px"></ControlStyle> 
      </asp:ImageField> 
    </Columns> 

Handler Изображение:

public void ProcessRequest(HttpContext context) 
    { 
     Byte[] productImage; 
     if (context.Request.QueryString["ProductImageId"] != null) 
     { 
      int productImageId = Convert.ToInt32(context.Request.QueryString["ProductImageId"]); 

      productImage = ProductImageBL.GetImage(productImageId); 

      if (productImage != null) 
      { 
       context.Response.BinaryWrite(productImage); 
       context.Response.End(); 
      } 
     } 
    } 

ProductImageBL GetImage Метод:

public static Byte[] GetImage(int productImageId) 
    { 
     SqlCommand cmd = new SqlCommand(); 
     cmd.CommandText = "Select ProductImage from ProductImages where ProductImageId = @ProductImageId"; 

     cmd.Parameters.AddWithValue("@ProductImageId", SqlDbType.Int).Value = productImageId; 

     SqlDataReader dataReader = DbUtility.GetDataReader(cmd); 
     if (dataReader.HasRows) 
     { 
      dataReader.Read(); 
      return (Byte[])dataReader[0]; 
     } 
     else 
     { 
      return null; 
     } 
    } 

Пожалуйста, помогите мне с этим кодом.

Спасибо заранее.

+1

Пожалуйста, измените вопрос, чтобы включить разделы _relevant_ вашего кода. Это сообщество не относится к внешним ссылкам к коду, и ваш вопрос гораздо более вероятен для получения ответов, если вы предоставляете код напрямую. –

ответ

0

Создать страницу для целей изображений говорят GetMeImage.aspx с функцией ниже

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Request.QueryString["ProductImagesID"] != null) 
    { 
string query = "Select ProductImage from ProductImages where ProductImageId = @ProductImageId"; 
SqlCommand cmd = new SqlCommand(query,YourConnectionObject); 

    cmd.Parameters.AddWithValue("@ProductImageId", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["ProductImagesID"]); 

    SqlDataAdapter adapter = new SqlDataAdapter(cmd); 
    DataTable dt = new DataTable(); 

    if (dt != null) 
    { 
     Byte[] bytes = (Byte[])dt.Rows[0]["Data"]; 
     Response.Buffer = true; 
     Response.Charset = ""; 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     Response.ContentType = dt.Rows[0]["ContentType"].ToString(); 
     Response.AddHeader("content-disposition", "attachment;filename=" 
     + dt.Rows[0]["Name"].ToString()); 
     Response.BinaryWrite(bytes); 
     Response.Flush(); 
     Response.End(); 
    } 
} 
} 

В GridView поле Шаблона надстройки

<asp:ImageField DataImageUrlField = "ProductImagesID" 
      DataImageUrlFormatString = "../GetMeImage.aspx?ProductImagesID={0}" 
      ControlStyle-Width = "75" ControlStyle-Height = "75" 
      HeaderText = "Preview Image"> 
       <ControlStyle Height="100px" Width="100px"></ControlStyle> 
      </asp:ImageField> 
Смежные вопросы