ASP.NET: DataList - how to edit 2 vars in Item in ItemTemplate
I have a datalist, and on the ItemTemplate, I do this for example:
<%#Eval ("MinAge") 开发者_运维百科%>
Where MinAge is a Int. I also have a MaxAge that is also an int.
Quesiton is, how do i change it so that i could do something like:
if (MaxAge == 99)
MinAge + "+"
else
MinAge + "-" + MaxAge
so that if we have minage=18,maxage=99 it will be 18+ if we have minage=18,maxage=20 it will be 18 - 20
the thing is it gets complicated for me because i try to change int to string, so what is the proper way of doing it?
In your codebehind do...
protected string GetAgeRange(object minAge, object maxAge)
{
var min = (int)minAge;
var max = (int)maxAge;
if (max >= 99)
return min + "+";
return min + " - " + max;
}
Then replace your
<%# Eval("MinAge") %>
with
<%= GetAgeRange(Eval("MinAge"), Eval("MaxAge")) %>
Note the use of = instead of #.
You'll want some more error checking in GetAgeRange but the general idea should be what you need.
精彩评论