Eval 数据绑定

<%# Regex.Replace((string)Eval("IP"), @"\.\d+$", ".*") %> 

Eval内部必须是双引号,因为它是普通的c#方法。
Eval可以使用第二个参数格式化,因此例如你就可以写:
<%# Eval("ID","~/DelegateConfirm.aspx?id={0}") %>
<%# 表达式%>---------<%#sum/10 %>
Barcode字段存储的是条形码号,如果条形号码为空,则显示"待审核",否则显示条形码
<%#Eval("Barcode").Equals("") ? "<font color='red'>待审核</font>" : "<img src='http://www.mywebsite.com/barcode/barcode.dll?id="+Eval("Barcode")+"'/>"%>
将格式化日期的方法绑定到数据控件中
protected string GetTime(object time)
{ return Convert.ToDateTime(time).ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
然后,将自定义方法GetTime,绑定到数据控件GridView中的显示日期列上,其代码如下:
<%# GetTime(DataBinder.Eval(Container.DataItem, "POSTTIME"))%>
格式化时间并进行绑定
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView drv = ds.Tables["tbOrder"].DefaultView[i];
DateTime dt = Convert.ToDateTime(drv["EDate"]);
GridView1.Rows[i].Cells[9].Text = dt.ToLongDateString( );
}
高亮:
public static string HighLight(string instr, bool light)
{
if (light)
{
instr = "<span style='color:red'>" + instr + "</span>";//要加亮的文本,Red
}
else
{
instr = "<span style='color:blue'>" + instr + "</span>";//要加亮的文本,Blue
}
return instr;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[4].Text == "False")
{
e.Row.Cells[4].Text = HighLight("未审核", true);//Red
}
else
{
e.Row.Cells[4].Text = HighLight("已审核", false);//Blue
}
}
}

 
int 遇到 null :

 <td align="center">
<%
# Eval("SpecAppend.Result3"== null ? "<font color='#ff0066'>未返回</font>" : 
Convert.ToInt32(Eval(
"SpecAppend.Result3")) == 2 ? "<font color='red'>失败</font>" : 
"<font color='blue'>成功</font>"  %></td>  


避免Object cannot be cast from DBNull to other types. 错误

  <%# Eval("Sex").GetType() == Type.GetType("System.DBNull") ?"未设置": Convert.ToInt32(Eval("Sex")).Equals(0)?"<font color='blue'>女</font>" : "<font color='green'>男</font>"%>

 

 ImageUrl='<%# "../HotShopImg/"+DataBinder.Eval(Container.DataItem,"ImgURL") %>'


------Eval("picture").ToString()----记得加").ToString() 不然会提示object无法转换string

<href='<%# DataBinder.Eval(Container.DataItem,"url") %>'>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("picture").Equals("")?"http://www.princehall.com.cn/img/no_img.gif":
 Eval("picture").ToString().Substring(0, Eval("picture").ToString().LastIndexOf(".")) + "C" 
+ Eval("picture").ToString().Substring(Eval("picture").ToString().LastIndexOf(".")) %
>' /></a>


Text='<%# Bind("price0", "{0:N2}") %>'

 

 

ip:1.1.1.1---1.1.1.*

<%# Regex.Replace((string)Eval("IP"), @"\.\d+$", ".*") %>

 

//      隐藏IP。
//      参数:
//      ip : 需要隐藏的IP。
//      n  : 隐藏的位数。
public  static  string  IP( string  ip,  int  n)

  
if ( string .IsNullOrEmpty(ip))
   {
    
return  string .Empty;
   }
 
string [] ary  =  ip.Split( ' . ' );
 
int  length  =  Text.GetArrayLength(ary);
 
string  result  =  ary[ 0 ];
 
for  ( int  i  =  1 ; i  <  length; i ++ )
  {
     
if (i  +  1  >   length  -  n)
      {
         result 
=  result  +  " .* "
      }
     
else
       {
          result 
=  result  +  " . "  +  ary[i];
        }
    }
  
return  result;
 }

Text.IP("192.168.0.1", 1);  结果 192.168.0.* 
Text.IP("192.168.0.1", 2);  结果 192.168.*.* 
Text.IP("192.168.0.1", 3);  结果 192.*.*.* 

 

 

 

  public string strphone(string phone)

        {
            string reg = phone.Substring(phone.Length - 8, 5);
            phone = phone.Replace(reg, "*****");
            return phone;
        }

137*****432           <%# strphone( Eval("phone").ToString()) %>  

 

使用Eval数据绑定时提示:字符文本中的字符太多

错误的  Text="<%# Eval('ProductID') %>">

正确的  Text='<%# Eval("ProductID") %>'>



    本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2009/09/23/1572343.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值