Click here to Skip to main content
15,885,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello ,

How to find image control in grid view row data bound event
Posted

Please look at the Control.FindControl Method. Here[^] is the MSDN link.

An example usage will be:
C#
Label myLabel = (Label)myGridView.Rows[myGridView.SelectedIndex].Cells[2].FindControl("myLabel"); 


Good luck,
OI
 
Share this answer
 
Comments
srishti_ 2-May-13 2:15am    
i am not asking this i m asking for image control just lika as label inside gridview find as
Label lblip = (Label)e.Row.FindControl("lblIPRestriction");
Orcun Iyigun 2-May-13 2:30am    
And what are you asking for? Be clear! If label does not work change it with the proper control (ImageControl)?
Thanks7872 2-May-13 2:52am    
I think she clearly asked How to find image control.She was clear at her end.
Orcun Iyigun 2-May-13 2:59am    
And I gave her the answer. Looking at your answer your solution also advises FindControl method. She has to do the implementation...
Try this

C#
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        (Image) img=((Image)e.Row.FindControl("Image control id"));
    }
}
 
Share this answer
 
v2
E.g. you have Gridview1 with 3 columns picturid,picturname,picture.
ASP.NET
<asp:templatefield xmlns:asp="#unknown">
      <itemtemplate>/*3rd column of gridview*/
        <asp:image id="Image1" imageurl="~/images/ab.jpg" runat="server" />
      </itemtemplate>
    </asp:templatefield>

RowDataBound event of your gridview:
C#
protected Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
            Image desired_image = (Image)e.Row.Cells[2].FindControl("Image1 ");
     }
}
 
Share this answer
 
v3
Comments
srishti_ 2-May-13 2:27am    
ok and how to set image url dynamic in image control
Thanks7872 2-May-13 3:03am    
Have a look at this link.It gives some look at the issue as well as the consequences.Displaying images dymically is Briefly described here
Hi...
see this.

XML
<asp:templatefield xmlns:asp="#unknown">
      <itemtemplate>/*3rd column of gridview*/
        <asp:image id="Image1" imageurl='<% .aspx?id="img"%>'; runat="server" />
      </itemtemplate>
    </asp:templatefield>


thank u
 
Share this answer
 
Hi...
I think this is right one to it.

VB
<asp:ImageField AlternateText="ImageUrl" DataAlternateTextField="ImageUrl"
                    DataImageUrlField="ImageUrl" HeaderText="ImageUrl" ControlStyle-Width="100px">


thank u.
 
Share this answer
 
For binding images to GridView, use Handler Class. It is the best practice.
You just need to call the class on ImageUrl prperty of Image Control like below.
XML
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("ImageID") %>' Height="150px" Width="150px"/>

And get the image from DataBase in that Handler only.

Follow the below articles for implementations and code.
1. how to insert images into database and how to retrieve and bind images to gridview using asp.net (or) save and retrieve images from database using asp.net[^].
2. Display Images In GridView From DataBase Asp.Net[^].
3. Inserting Images into Database and Display it in GridView through Handler.ashx[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900