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 :
<%#?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。
//?????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") %>'>
?
總結
- 上一篇: WPF学习笔记 - 不一样的皇帝的新衣
- 下一篇: 按 结构记录的 相关字段 快速排序