ASP.NET中使用Eval()函数绑定数据时,不能使用IF进行逻辑判断的解决方案

转自:http://www.cnblogs.com/jinho/archive/2010/04/17/1714317.html

 

 

很多次,我在使用ASP.NET数据绑定控件中绑定数据时 使用if语句进行逻辑判断!但是肯定那是失败了!没有办法,既然遇到了这个棘手问题,先Google,baidu一下吧! 其他人也有此想法,虽然方案不是很好,但我还是记录下来吧!以便以后查看。

先来说一说:<%# Eval(“Field”)%> 吧!

我们在控件中直接用<%# Eval(“Field”)%>,那他应该相当于:<%="str”%>和<% respose.write(“str”)%> 吧!因为在控件中绑定后他值就直接显示了!相当于输出咯!

我曾经多少次想这样写:<%# if(Eval(“Field”)=="str”){...}else{...}%> 可惜这样他不行啊!

1.如果简单的话可以使用三元运算符,<%# Eval(“Field”)=="str”?"somecode":"somecode"%>

但是如何if判断后处理很复杂呢?显然三元运算符不是很好!那样页面代码很多,我们毕竟不是在做ASP,也不好看,美工也不好写样式!

2.在后台页面写逻辑代码,返回字符串

<span style="color:blue;line-height:19px">public void </span>Handler(<span style="color:blue;line-height:19px">string </span>str)
    {
        <span style="color:blue;line-height:19px">if </span>(str.Length > 5000)
        {
            Response.Write(<span style="color:#a31515;line-height:19px">"<div title='" </span>+ str + <span style="color:#a31515;line-height:19px">"'>" </span>+ str.Substring(0, 1000) + <span style="color:#a31515;line-height:19px">"</div>"</span>);
        }
        <span style="color:blue;line-height:19px">else
        </span>{
            Response.Write(str);
        }

    }
    <span style="color:green;line-height:19px">// or
    </span><span style="color:blue;line-height:19px">public string </span>Handler(<span style="color:blue;line-height:19px">string </span>str)
    {
        <span style="color:blue;line-height:19px">if </span>(str.Length > 5000)
        {
            <span style="color:blue;line-height:19px">return </span><span style="color:#a31515;line-height:19px">"<div title='" </span>+ str + <span style="color:#a31515;line-height:19px">"'>" </span>+ str.Substring(0, 1000) + <span style="color:#a31515;line-height:19px">"</div>"</span>;
        }
        <span style="color:blue;line-height:19px">else
        </span>{
            <span style="color:blue;line-height:19px">return </span>str;
        }
    }

 

前台调用:

<span style="line-height:19px; background-color:rgb(255,238,98)"><%</span><span style="color:blue;line-height:19px"># </span>Handler(Eval(<span style="color:#a31515;line-height:19px">"Field"</span>).ToString()) <span style="line-height:19px; background-color:rgb(255,238,98)">%></span>
注意Eval还可以绑定对象.属性 如:<span style="line-height:19px; background-color:rgb(255,238,98)"><%</span><span style="color:blue;line-height:19px"># </span>Handler(Eval(<span style="color:#a31515;line-height:19px">"User.Name"</span>).ToString()) <span style="line-height:19px; background-color:rgb(255,238,98)">%></span>
3.摘自网络

<% int _nIndex=0; %> <!--定义一个临时的整型变量-->
<asp:Repeater ID="_TopicRepeater" Runat="SERVER" DataSource="...">
<ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Title")   %>
    <%# DataBinder.Eval(Container.DataItem, "Author")  %>
    <%# DataBinder.Eval(Container.DataItem, "Clicked") %>
    <%# DataBinder.Eval(Container.DataItem, "ReCount") %>
<%
int nReCount=(int)(((DataView)_TopicRepeater.DataSource).Table.Rows[_nIndex++]["ReCount"]);
// 也可以分成几句来写
// DataView DV=(DataView)_TopicRepeater.DataSource;
// DV.Table.Rows[_nIndex++]["ReCount"];
if(nReCount==0) { %>
     ----
<% } else { %>
    <%# DataBinder.Eval(Container.DataItem, "LastReplyer") %>
<% } %>
</ItemTemplate>
</asp:Repeater>

来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/afritxia/archive/2004/10/22/146936.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值