Eval 資料繫結

來源:互聯網
上載者:User

<%# 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

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.