现在的位置: 首页 > 综合 > 正文

关于数据绑定的一些小技巧 .

2012年11月07日 ⁄ 综合 ⁄ 共 1942字 ⁄ 字号 评论关闭

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

------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+$", ".*") %>

 

使用Eval数据绑定时提示:字符文本中的字符太多

错误的  Text="<%# Eval('ProductID') %>">

正确的  Text='<%# Eval("ProductID") %>' >

抱歉!评论已关闭.