ASP.NET在綁定時使用條件運算式

來源:互聯網
上載者:User
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_r("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_r("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_r("Title") %></td> 
 11:  </tr> 
 12:  </ItemTemplate> 
 13:  <FooterTemplate> 
 14:  </table> 
 15:  </FooterTemplate> 
 16: </asp:Repeater> 

http://blog.sina.com.cn/s/blog_3e29b20b0100il16.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.