• 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

  • 相关阅读:
    获取网页可视区域的宽高,不包括工具栏和滚动条(浏览器窗口可视区域大小)
    display:inline-block 来解决盒子高度不一样,造成的盒子浮动
    Jquery UI 中的datepicker() ,获取日期后的回调函数onClose()
    删除url搜索带的参数
    判断表单是否需要校验时可以按照类似逻辑去处理
    去除url中锚点的方法
    js常面试基础题
    vue项目总结
    vue-router
    回流与重绘
  • 原文地址:https://www.cnblogs.com/wintersun/p/1714827.html
Copyright © 2020-2023  润新知