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,如需转载请自行联系原作者


相关文章
|
2月前
|
存储 监控 JavaScript
Vue之插值表达式,v-bind(单向绑定),v-model(双向绑定)
Vue之插值表达式,v-bind(单向绑定),v-model(双向绑定)
|
9月前
|
JavaScript
Vue中如何使用v-model双向数据绑定select、checked等多种表单元素
Vue中如何使用v-model双向数据绑定select、checked等多种表单元素
192 0
|
9月前
|
前端开发 JavaScript
前端eval的使用
前端eval的使用
59 0
|
11月前
|
JavaScript 前端开发
Vue —— 基础(零)(模板语法、数据绑定、el/data写法、MVVM模型)
Vue —— 基础(零)(模板语法、数据绑定、el/data写法、MVVM模型)
|
12月前
|
JavaScript 前端开发
Vue.js事件修饰符及v-model双向数据绑定
Vue.js事件修饰符及v-model双向数据绑定
126 0
|
前端开发 JavaScript UED
Vue3 你可能忽略的 v-model 用法(一)
Vue3 你可能忽略的 v-model 用法
Vue3 你可能忽略的 v-model 用法(一)
|
JavaScript
Vue 中v-model的完整用法(v-model的实现原理)
本章学习Vue 中v-model的完整用法。
466 0
|
JavaScript
Vue指令 v-model实现 双向数据绑定
Vue指令 v-model实现 双向数据绑定
Vue指令 v-model实现 双向数据绑定