inline code in a server control

G

Guest

I have a datalist bound to a dataset.

In the Items template I have a Image which was supposed to changed depending
on a certain value retrieved from the dataset.

I have the ImageUrl attribute for the Image which I want to change

ImageUrl='...images\' <%# DataBinder.Eval(Container,
"DataItem.NeedForCover")%>'.BMP'> but it complains: the server tag is not
well formed. All I want to get from the dataset is the string.

How can I make it work ?

Also is there a way to put some kind of condition before an attribute like
we did with ASP f.e:

<%#if DataBinder.Eval(Container, "DataItem.NeedForCover")=True then %>
ImageUrl='...\images\HAPPY.BMP'>
<%# else %>
ImageUrl='...\images\SAD.BMP'>
<%# End if %>



Please help,

Thank everyone.
 
N

Nathan Sokalski

One thing you can use is the ItemDataBound event, which is fired after each
event. Also, if you are simply going to append data from the datasource
(which is what it looks like you are doing for the ImageUrl attribute, try
this overload of the DataBinder.Eval method:

ImageUrl='<%#
DataBinder.Eval(Container,"DataItem.NeedForCover","images/{0}.BMP") %>'

This code would return a string such as:

ImageUrl='images/yourimagename.BMP'

It basically replaces the {0} with the specified DataItem field.


If you have more complex strings you want to create, you can do that using
the ItemDataBound event. Here is an example of how to do the same thing
using this event:

Private Sub datMembers_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
datMembers.ItemDataBound
CType(e.Item.FindControl("imgYourImageID"),
System.Web.UI.WebControls.Image).ImageUrl="images/" &
e.Item.DataItem("NeedForCover") & ".BMP"
End Sub

Obviously you can use any type of code you want in this event, including
conditional statements. This event is triggered after the individual Item
has been databound, so it can also be used to check what value was assigned
from the datasource (sometimes you want to check for a value of "" or maybe
replace "" with "&nbsp;" or a default value, or any number of other things
you may want to do). Hopefully you can solve your problems using these
techniques, if you need more help feel free to ask. Good Luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top