Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display image based on another templatefield value.

Example person num(0010200) and image saved in folder is 00102000.jpg , if person num(01234) and image saved in folder is 01234.jpg, etc. like this i have many employee images.and this all saved in /img/ webfolder.

Now when ever lable2 person num changes i need to change the image1 which will be same as person num,

please suggest me to achieve the same. example code below.

What I have tried:

<asp:TemplateField HeaderText="person_num" SortExpression="person_num">
<asp:BoundField DataField="full_name" HeaderText="full_name" SortExpression="full_name" />
<asp:TemplateField HeaderText="Image">
Posted
Updated 18-Sep-16 10:25am
v2

First of all, I consider you want to have a small change in the scenario you have presented in the question i.e,
Quote:
Example person num(0010200) and image saved in folder is 00102000.jpg , if person num(01234) and image saved in folder is 01234.jpg

It should be either
-- person_num = 0010200 then image = 0010200.jpg (note: 2 zeros at the end instead of 3 as stated in the question considering it to be a typo)
person_num = 01234 then image = 01234.jpg
-- person_num=0010200 then image = 00102000.jpg (note: 1 more zero added at the end, same as question)
person_num = 01234 then image = 012340.jpg (1 more zero added to at the end)

For the first case if you are saving the images as the same name as person_num you can use following code without any other logic added to it.
HTML
<itemtemplate headertext="Image">
     <img src="<%# Eval("person_num","~/Images/{0}.jpg") %>" <br="" mode="hold"> </itemtemplate>

For the second case, you need just very simple change i.e,
HTML
<itemtemplate headertext="Image">
     <img src="<%# Eval("person_num","~/Images/{0}0.jpg") %>" <br="" mode="hold"> </itemtemplate>


Please let me know if your requirement is something else.
Hope, it helps :)
 
Share this answer
 
Comments
Karthik_Mahalingam 18-Sep-16 13:41pm    
5
Suvendu Shekhar Giri 18-Sep-16 14:08pm    
Thanks a lot @Karthik :)
Excellent...some what same. and i got the result almost your idea,

<asp:image id="Image2" runat="server" imageurl="<%# string.Format("~/img/{0}.jpg",Eval("person_Num"))%>" height="300" width="300">
 
Share this answer
 

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