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") %>'>

 

posted @ 2009-09-23 10:48  曾祥展  阅读(2699)  评论(1编辑  收藏  举报