Ajit N

Ajit N

  • 1.6k
  • 324
  • 11k

How to Show Image on gridview image button click event ?

Jun 29 2019 2:44 AM
Hello, I want to show image in new tab when i click on particular image button in GridView. i tried some code which i mentioned below,but it didn't work for me. so help me how can i do this.
Front End Code : 
  1. <asp:GridView ID="GridView1" runat="server" CssClass="table table-bordered table-hover" DataKeyNames="ID" OnRowCommand="GridView1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" EmptyDataText="No records has been added." AutoGenerateColumns="False">  
  2.   
  3.                                <Columns>  
  4.                                    <asp:BoundField DataField="ID" HeaderText="ID" />  
  5.                                     
  6.                                      
  7.   
  8.                                    <asp:TemplateField HeaderText="Photo">  
  9.                                        <ItemTemplate>  
  10.                                            <asp:ImageButton ID="imgRedirect" runat="server" Height="50px" Width="50px" ImageUrl='<%# Eval("img") %>' OnClick="imgRedirect_Click" />  
  11.                                        </ItemTemplate>  
  12.                                    </asp:TemplateField>  
  13.   
  14.                                      
  15.                                </Columns>  
  16.   
  17.                            </asp:GridView>  
 Backend Code :
  1. protected void imgRedirect_Click(object sender, ImageClickEventArgs e)  
  2.    {  
  3.       int getid = Convert.ToInt32(GridView1.Rows[GridView1.SelectedIndex].Cells[0].Text);  
  4.        string imagename = string.Empty;  
  5.   
  6.        con.Open();  
  7.        SqlCommand cmd = new SqlCommand("select * from AddPrescription where ID='"+ getid + "'", con);  
  8.        SqlDataReader dr = cmd.ExecuteReader();  
  9.        if (dr.Read())  
  10.        {  
  11.            imagename = dr["img"].ToString();  
  12.             
  13.        }  
  14.        con.Close();  
  15.        Response.Redirect(imagename);  
  16.   
  17.    }  
 

Answers (1)