c# - Setting ImageSrc for each template -
so in confusing situation right now, have listview itemtemplate contains image element:
<asp:listview id="listview1" runat="server"> <itemtemplate> <asp:image id="image1" class="main" runat="server" imageurl='<%# eval("photo1") %>' /> </itemtemplate> </asp:listview>
in click event have following code:
protected void btn_search_click(object sender, eventargs e) { var img = listview1.items[0].findcontrol("image1") image; var lbl = listview1.items[0].findcontrol("lbl_id") label; string image = img.imageurl; foreach (listviewitem item in listview1.items) { if (img.imageurl == "defaultcar.jpg") { img.imageurl = "images/defaultcar.jpg"; } else { img.imageurl = "images/" + "44/" + image; } } }
so in db have 2 images, 1 called "defaultcar.jpg" , called "car.jpg" trying create correct path each image, have debugged , seems first itemtemplate displaying "images/44/defaultcar.jpg" , second itemtemplate displaying "car.jpg", should other way round, "images/44/car.jpg" , "images/defaultcar.jpg"
does know doing wrong?
if understand issue, can solve below:
first create method return image url like:
protected string getimageurl(object imageurl) { if(imageurl == null) return ""; if(imageurl.tostring() == "defaultcar.jpg") return = "images/defaultcar.jpg"; else return "images/44/" + imageurl.tostring(); }
then update .aspx file:
<asp:image id="image1" class="main" runat="server" imageurl='<%# getimageurl(eval("photo1")) %>' />
Comments
Post a Comment