Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello helpers,

I have a we gridview that contains some data on drivers. I am successfully able to get some data in a column when selected.
I also have driver images in a folder and am also able to successfully construct a valid imageurl from a value in the datagrid
to pick an image in the folder.

e.g grid view contains an id field called 1 so when user selects that row, I am able to get 1 and construct an imageurl like

C:/webapp/images/1.jpg

Initially I used ajax to display the image in an image control but it was not showing so now, I have being trying to do same with page refreshes and no ajax but it still does not display in the image control.

Please see some codes below...(version without ajax-updatePanel)
Please am i missing something ?

At least if this version works I may know how to Ajax enable it

Thanks for response
C#
protected void Page_Load(object sender, EventArgs e)
      {
          //populate gridview with drivers data

          var dg=new dalConn();
          dgdrivers.DataSource = dg.Fromdbase("select * from getdrivers");
          dgdrivers.DataBind();

          dvimage.ImageUrl = lblfullname.Text;
      }

      protected void dgdrivers_SelectedIndexChanged(object sender, EventArgs e)
      {

         lblfullname.Text = Server.MapPath("/dImages/" + dgdrivers.SelectedRow.Cells[1].Text.ToString() + ".jpg");

      }
Posted
Updated 15-Oct-11 21:52pm
v2

1 solution

First of all when you need to bind your data to image control at that time you need to bind it with using virtual path not actual path

if your project is located to c: and inside your project there is a folder named "dImages" and it will contain images to display then you need to write your image url like

C#
lblfullname.Text = "~/dImages/" + dgdrivers.SelectedRow.Cells[1].Text.ToString() + ".jpg"


inside your code you use "Server.MapPath", i think you need not get when this method is used..

Server.MapPath is used when you actually access your drives like C:,D: etc or you need to access physical path of your files.

for displaying images you need to use virtual path starts with "~/....." or "../...."

or after genrating image url try this..

Where is your Image Control Located inside grid view or out side grid view..


If it inside grid view then you need to write your code inside a databoud event like item data boud or row data bound,
at that event you need to cast your control dynamically and assign the ImageUrl
Like this

C#
((Image)GridView1.Rows[e.Row.RowIndex].FindControl("YOUIMAGECONTROLID")).ImageUrl = "";


and if it out side then directly you can assign image URL to your image control..

Like..
C#
YouIMAGEID.ImageUrl = "";


or even if the images is not displayed then after assigning imge url dynamically then fire "DataBind()" method of your control like this..
C#
YOURIMAGEID.DataBind();
 
Share this answer
 
v3

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