Show image of GridView to an img of an asp.net table

1

I want to pass an image of GridView to a table that has the IMG tag.

I was previously passing data from DataGridView to TexBox and it worked for me. Now the problem is that it can not be shown on that label.

This is the code I use to pass data:

protected void DtgSolicitudes_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        try
        {
            DtSet = new DataSet();        
            SqlConnection con = new SqlConnection(ObtenerCadenaConexion());
            con.Open();
            DtgSolicitudes.SelectedIndex = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = DtgSolicitudes.SelectedRow;
            SqlCommand cmd = new SqlCommand("SP_BuscarSeguimiento", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("OPT", 2);
            SqlParameter p2 = new SqlParameter("periodo", "");
            SqlParameter p3 = new SqlParameter("oficina", "");
            SqlParameter p4 = new SqlParameter("rubro", row.Cells[3].Text);

            cmd.Parameters.Add(p1);
            cmd.Parameters.Add(p2);
            cmd.Parameters.Add(p3);
            cmd.Parameters.Add(p4);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            TextBox1.Text = Convert.ToString(row.Cells[2].Text);
            TextBox2.Text = Convert.ToString(row.Cells[1].Text);
            TextBox3.Text = Convert.ToString(row.Cells[3].Text);
            TextBox4.Text = Convert.ToString(row.Cells[4].Text);
            TextBox5.Text = Convert.ToString(row.Cells[5].Text);
            TextBox6.Text = Convert.ToString(row.Cells[6].Text);
            TextBox7.Text = Convert.ToString(row.Cells[7].Text);
            TextBox8.Text = Convert.ToString(row.Cells[8].Text);
            TextBox9.Text = Convert.ToString(row.Cells[9].Text);

            Image1.ImageUrl = Convert.ToString(row.Cells[12].Text);
            var imagen = row.FindControl("img") as Image;

            con.Close();
        }
        catch (Exception Ex)
        {
            // DvError.Visible = true;
            Session["13"] = Ex.Message;
        }
    }
}

SQL SERVER MY TABLE

I get the image with that name byte blank:

    
asked by PieroDev 31.03.2017 в 22:15
source

2 answers

0

To convert the bit image into an img you have to declare the following script:

 <asp:ListView ID="ListImagenes" runat="server" Visible="true">
                                              <ItemTemplate>
                                             <asp:Image ID="Image1" ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("filecontent"))  %>' runat="server" Style="width: 150px; height: 150px" data-container="body" data-toggle="popover" data-placement="right" />

                                                                              <script>
                                                                                  $(document).ready(function () {

                                                                                      $('<%#  "#ListImagenes_Image1_" + Container.DisplayIndex %>').popover({
                                                                                          html: true,
                                                                                          trigger: 'hover',
                                                                                          content: '<img src="' + '<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("filecontent")) %>' + '" id="popover_img" Style="width: 500px" />'
                                                                                      }).hover(function () {
                                                                                          $('#popover_img').attr('src', $(this).data('img'));
                                                                                      });

                                                                                      $('<%#  "#ListImagenes_Image1_" + Container.DisplayIndex %>').mouseover(function () {
                                                                                          $('<%#  "#ListImagenes_Image1_" + Container.DisplayIndex %>').popover('show')
                                            });

                                                                                      $('<%#  "#ListImagenes_Image1_" + Container.DisplayIndex %>').mouseout(function () {
                                                                                          $('<%#  "#ListImagenes_Image1_" + Container.DisplayIndex %>').popover('hide')
                                            });
                                                                                  });

    

    
answered by 01.04.2017 / 16:52
source
1

In a web environment if the image is persisted in the database as array of byte you should retrieve it indirectly by means of a handler that sends in Response the image to show it in the img tag

In the article

[ASP.NET] GridView - Employee Edition

Download example code

I explain how you could achieve it.

The column that shows the image should be:

<asp:TemplateField HeaderText="Imagen">
        <ItemTemplate>
             <asp:Image ID="Image1" runat="server" 
                       ImageUrl='<%# Eval("IdEmpleado", "imagen.ashx?id={0}") %>' Width="100px" Height="100px" />
        </ItemTemplate>
</asp:TemplateField>

Where the ImageUrl points to the handler so that this by means of the id of the entity returns the array of byte in Response .

    
answered by 31.03.2017 в 23:27