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

你可能感兴趣的:(asp.net)