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