conditional in GridView Eval binding?

G

Guest

I'm trying to replace a user code in data column with user friendly text for
a GridView ItemTemplate as in:

Text='<%# Eval("LocationType")%> == "P" ? "Pickup" : "DropOff"'

However the value always displays "DropOff" as if the binding doesn't return
any P's but I know there are Ps in the rows I'm testing with.

LocationType is defined as char(1) in the table.

Any suggestion why this isn't working?

Thanks.
 
K

Karl Seguin [MVP]

Casing? Maybe this will do the trick:

<%# string.Compare(Eval("LocationType"), "P", false) == 0 ? "Pickup" :
"DropOff" %>

you could also try with single quotes in your example...maybe it's a char
<--> string thing, though I doubt it.

Karl
 
J

John Prado

Your code is wrong. You're mixing server and javascript in the same line.

Try do this in GridView_RowDataBound event

get the data bind for LocationType

do your friendly string

show it in the correspondent cell
 
G

Guest

I it very well may be the char vs string but I can't for the life of me
figure out the single quote setup in this context, as the bind string is
encapsulated with single
quotes ;)

You solution worked when casting the eval to a string as in:

string.Compare((string)Eval("LocationType"), "P",false)==0?"Pickup":"Dropoff"

Thanks much!
 
M

Michael Appelmans

Hello John,

Thanks for your suggestion. Managed to keep this all in the Gridview and
avoid code behind solution.

Michael
 
Joined
Dec 2, 2008
Messages
1
Reaction score
0
Perfect Solution for this is


<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lblUName" runat="server" Text='<%# String.Compare((string)Eval("LocationType"),"P",false)==0?"Pickup":"Dropoff" %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>




It will definitely works :)



From,
Pavan kumar.P
 
Joined
Aug 13, 2009
Messages
1
Reaction score
0
I have an issue using if statement in check box gridview

Here is my code:

<%# String.Compare((string)Eval("request"),"1",fa lse)==0?"<img src='imges001.png'>":"<asp:checkbox ID='s1' Runat='server' " %>


It doesn't show the checkbox, display nothing.... any suggestion. Thanks...
 
Joined
Jul 29, 2009
Messages
2
Reaction score
0
Try this====>

<input type="checkbox" <%# String.Compare((string)Eval("select_priv"),"t",fal se)==0?"checked=\"true\"":"" %> />


this one is working for me....
 
Joined
Sep 17, 2009
Messages
4
Reaction score
0
Hey,




you may try this :

Text='<%# Eval("LocationType").tostring() == "P" ? "Pickup" : "DropOff"'%>'



Comm100
__________________
Free chat software for you
Microsoft Certified Partner
.comm100.com
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top