ASP.NET在绑定时使用条件表达式

     Asp.net2.0不支持在数据绑定时的条件表达式。Asp.net 4.0已经平滑地解决这个问题,不用修改代码。
下面让我们来看一个简单的ASP.NET数据绑定场景,有这么一个Repeater:

   1:      <asp:Repeater runat="server" ID="itemsList">
   2:          <HeaderTemplate>
   3:              <table border="1" cellspacing="0" cellpadding="5">
   4:          </HeaderTemplate>
   5:          <ItemTemplate>
   6:              <tr>
   7:                  <td align="right">
   8:                      <%# Container.ItemIndex + 1 %>.
   9:                  </td>
  10:                  <td>
  11:                      <%# Eval("Title") %>
  12:                  </td>
  13:              </tr>
  14:          </ItemTemplate>
  15:          <FooterTemplate>
  16:              </table>
  17:          </FooterTemplate>
  18:      </asp:Repeater>


然后后端的cs:

   1:     protected void Page_Load(object sender, EventArgs e)
   2:          {
   3:              var items = new[] { 
   4:                      new { Id = 1, Title = "Headline 1" },
   5:                      new { Id = 2, Title = "Headline 2" },
   6:                      new { Id = 2, Title = "Headline 3" },
   7:                      new { Id = 2, Title = "Headline 4" },
   8:                      new { Id = 2, Title = "Headline 5" }
   9:                  };
  10:              itemsList.DataSource = items;
  11:              itemsList.DataBind();
  12:          }


当我们需要判断绑定时数据行,我们需要Create这么一个类似的方法或函数:

   1:          /// <summary>
   2:          /// Iifs the specified condition.
   3:          /// </summary>
   4:          /// <param name="condition">if set to <c>true</c> [condition].</param>
   5:          /// <param name="trueResult">The true result.</param>
   6:          /// <param name="falseResult">The false result.</param>
   7:          /// <returns></returns>
   8:          protected object Iif(bool condition, object trueResult, object falseResult)
   9:          {
  10:               return condition ? trueResult : falseResult;
  11:          } 


然后在ASPX中使用它:

   1:  <asp:Repeater runat="server" ID="Repeater1">
   2:    <HeaderTemplate>
   3:      <table border="1" cellspacing="0" cellpadding="5">
   4:      </HeaderTemplate>
   5:    <ItemTemplate>
   6:      <tr style='background-color: <%# Iif(Container.ItemIndex % 2==0, "white", "whitesmoke") %>'>
   7:        <td align="right">
   8:          <%# Container.ItemIndex + 1 %>.</td>
   9:        <td>
  10:          <%# Eval("Title") %></td>
  11:      </tr>
  12:    </ItemTemplate>
  13:    <FooterTemplate>
  14:      </table>
  15:    </FooterTemplate>
  16:  </asp:Repeater>


这是在模仿VB中的IIF函数。到了ASP.NET 4.0中,我们可以直接这么写了:

   1:  <asp:Repeater runat="server" ID="Repeater2">
   2:    <HeaderTemplate>
   3:      <table border="1" cellspacing="0" cellpadding="5">
   4:      </HeaderTemplate>
   5:    <ItemTemplate>
   6:      <tr style='background-color:<%# Container.ItemIndex % 2==0 ? "white" : "whitesmoke" %>'>
   7:        <td align="right">
   8:          <%# Container.ItemIndex + 1 %>.</td>
   9:        <td>
  10:          <%# Eval("Title") %></td>
  11:      </tr>
  12:    </ItemTemplate>
  13:    <FooterTemplate>
  14:      </table>
  15:    </FooterTemplate>
  16:  </asp:Repeater>


好了,这个演示是基于ASP.NET WebForm 数据绑定时条件表达式的实现。希望这篇POST对您有帮助!

Author: Petter Liu   http://wintersun.cnblogs.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值