How do I have an IF condition in a datalist?

zatuzik

New Member
How do I apply an IF condition to a row that wil be displayed in a datalist? i.e<BR><BR><% if #DataBinder.Eval(Container.DataItem, "hasRead") = "Yes" THEN %><BR><td><BR><span> <%# DataBinder.Eval(Container.DataItem, "hasRead")%> </span><BR></td><BR><% end if %><BR><BR>I have tried all Page.DataBind etc. but it just keeps giving me errors.You could use the onItemCreated event for the datalist which declares like this...<BR><BR><BR><BR>Sub Item_Created(sender As Object, e As DataListItemEventArgs)<BR><BR> Label1.Text = Label1.Text & " " & e.Item.ItemIndex.ToString()<BR><BR>End Sub 'Item_Created<BR><BR>Then use onItemCreated="Item_created" in your datalist declaration. Then you could manipulate the individual textbox...<BR><BR>Or... No... better yet.. Write a function...<BR><BR>Function DoStuff(HasRead as boolean)<BR> If HasRead then<BR> return "something"<BR> Else<BR> return ""<BR> End if<BR>End function<BR><BR>' Then use it like this...<BR><%# DoStuff(DataBinder.Eval(Container.DataItem, "hasRead"))%> <BR><BR>Or you could inherit baseDataList and make your own datalist control :pThanks very much, but If my database field type is a string it works fine, if I make my db field type a YesNo it gives me...<BR><BR>The operator is not valid for types 'Boolean' and 'String'. <BR><BR>Here's my function...<BR><BR>Function itemHasBeenRead(currentRecord)<BR> if currentRecord = "No" THEN<BR> return "font-weight: bold;"<BR> end if<BR>End Function<BR><BR>and I call function with...<BR><BR><%# itemHasBeenRead(DataBinder.Eval.Container.DataItem , "isRead")) %>have you tried:<BR><BR>if currentRecord = No THEN 'without quotes<BR><BR>or<BR><BR>if currentRecord = false THEN<BR><BR>or<BR><BR>if currentRecord = 0 THEN<BR><BR>??&nbsp;<BR>if currentRecord = FALSE THEN<BR><BR>END IF<BR><BR>You can convert pretty much anything to string.. even true false.. Im not exactly sure what a boolean value converts to when it converts to string. Convert it like this...<BR><BR>Ctype(CurrentRecord,String) or<BR>CurrentRecord.toString() ' pretty sure that works.<BR><BR>That will help you keep consistancy of the function. VB.net is strongly typed. Thats just another learning curve.Silly me!! Thanks it works now... I'm not really handling all this new way of ASP.NET but I'll just keep pushing and and as soon as I grasp the workings and fundamentals all will be ok.<BR><BR>Thanks Again
 
Back
Top