zoukankan      html  css  js  c++  java
  • 关于数据绑定的一些小技巧

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

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

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

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

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

  • 相关阅读:
    bzoj 1444: [Jsoi2009]有趣的游戏【AC自动机+dp+高斯消元】
    bzoj 3270: 博物馆【dp+高斯消元】
    bzoj 3105: [cqoi2013]新Nim游戏【线性基+贪心】
    bzoj 1923: [Sdoi2010]外星千足虫【高斯消元】
    bzoj 3629: [JLOI2014]聪明的燕姿【线性筛+dfs】
    bzoj 1296: [SCOI2009]粉刷匠【dp+背包dp】
    bzoj 3329: Xorequ【数位dp+矩阵乘法】
    bzoj 1306: [CQOI2009]match循环赛【dfs+剪枝】
    bzoj 4720: [Noip2016]换教室【期望dp】
    bzoj 2257: [Jsoi2009]瓶子和燃料【裴蜀定理+gcd】
  • 原文地址:https://www.cnblogs.com/wpf123/p/2123326.html
Copyright © 2011-2022 走看看