现在的位置: 首页 > 综合 > 正文

.NET 2.0中的Eval使用方法总结(未完成,续)

2012年05月13日 ⁄ 综合 ⁄ 共 2371字 ⁄ 字号 评论关闭
一、使用条件,
1、当绑定字段为bit型,需要根据判断显示内容,做如下处理。
(1)、在前台ASPX页面的处理
<%# Eval("Ischeck").ToString()=="True"?"[已鉴定]":"[未鉴定]"%>
再变通一下:
<%# Auction.Helper.CommonHelper.CutString((Eval("JudgeProductTitle").ToString()), 8)%>
 <%# Eval("Ischeck").ToString() == "True" ?  "<font color=\"red\">[已鉴定]</font>" :  "<font color=\"blue\">[未鉴定]</font>"%>
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
想说明的问题是,可以灵活的根据需要,来加载显示需要的字段内容,在一个<%# %>中,达到这样的效果。另外需要注意的地方是,如果根据条件,显示不同字段,可以用上面的方式变通处理,如果需要在其中连接固定字符串和需要显示不同条件的字段,请注意使用“+”号来连接。
另外今天随便试了一下,Eval中既然支持这种用法"<font color=\"red\">[已鉴定]</font>,郁闷~^)^~以前都是在后台程序里控制,写好多的HTML程序生成代码来控制这种行为特征的需要!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
(2)、在SQL语句中处理
变通方法:可以在取出字段时候,在SQL语句中做处理

使用 CASE

CASE 函数是特殊的 Transact-SQL 表达式,它允许按列值显式可选值。数据中的更改是临时的,没有对数据进行永久更改。例如,CASE 函数可以在 state 列中有 CA 值的行的查询结果集内显示 California。

CASE 函数包含:

  • CASE 关键字。
  • 需要转换的列名称。
  • 指定要搜索的表达式的 WHEN 子句和指定要替换它们的表达式的 THEN 子句。
  • END 关键字。
  • 可选的、定义 CASE 函数别名的 AS 子句。

下面示例在查询结果集内显示每个作者所居住州的全名:

SELECT au_fname, au_lname, 
   CASE state
      WHEN 'CA' THEN 'California'
      WHEN 'KS' THEN 'Kansas'
      WHEN 'TN' THEN 'Tennessee'
      WHEN 'OR' THEN 'Oregon'
      WHEN 'MI' THEN 'Michigan'
      WHEN 'IN' THEN 'Indiana'
      WHEN 'MD' THEN 'Maryland'
      WHEN 'UT' THEN 'Utah'
        END AS StateName
FROM pubs.dbo.authors
ORDER BY au_lname



(3)、绑定用Eval时,如何判断他是否为空

用Convert.IsDBNull
如下面代码:
SelectedValue='<%# Convert.IsDBNull(Eval("提现方式"))?"请选择提现方式":Eval("提现方式") %>'
(4)、可以使用自定义的方法(函数)在前台页面上处理上面提到的方法1和方法3的问题。


(5)、附一文章(地址:http://blog.sina.com.cn/s/blog_414cc36d01000933.html
具体内容如下

Maybe this is obvious, but it wasn’t obvious to me. I’m binding
some data in a repeater that has the following output based on two
numeric columns in my database. It doesn’t matter why or what the
data represents. It’s just two pieces of data
with some formatting:
42, (123) 
Basically these are two measurements. Initially, I would databind
this like so:
<%# Eval("First") %>, (<%# Eval("Second") %>)
The problem with this is that if the first field is null,
I’m left with this output.
, (123) 
Ok, easy enough to fix using a format string:
<%# Eval("First", "{0}, ") %>(<%# Eval("Second") %>)
But now I’ve learned that if the first value is null,
the second one should be blank as well. Hmm...
I started to do it the ugly way:
<%# Eval("First", "{0}, ") %>
<%# Eval("First").GetType() == typeof(DBNull) ? "" : Eval("Second", "({0})")%>
*Sniff* *Sniff*. You smell that too? Yeah, stinky and hard to read. Then it occured to me to try this:
<%# Eval("First", "{0}, " + Eval("Second", "({0})")) %>
Now that code smells much much better!
I put the second Eval statement as part of the format string
for the first. Thus if the first value is null,
the whole string is left blank. It’s all or nothing baby!
Exactly what I needed.

抱歉!评论已关闭.