簡體   English   中英

設計視圖ListView ASP.NET Logic

[英]Design View ListView ASP.NET Logic

我試圖在Asp.Net ListView的源代碼視圖中輸入邏輯。 問題在於,執行“ If(isItTrue(test))”時,程序在屏幕上寫入的是false還是true。 有誰知道如何解決這個問題?

   <%# test= Eval("testId")%>
          <%
              If (isItTrue(test)) Then

              %>
           <asp:Button ID="btnTest"  runat="server" Text="Like" />
           <%
           Else
               %>
               <asp:Label runat="server" Text="hello" </asp:Label>

           <%
           End If
               %>

您可以使用ItemDataBound檢查類似的信息,並根據您的條件顯示或隱藏控件。 在您的代碼中嘗試以下操作:

protected void ListViewTest_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    // if it is data item
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        // call your function
        if (isItTrue("test"))
        {
            // show the button
            e.Item.FindControl("btnTest").Visible = true;
        }
        else 
        {
            // show the label
            e.Item.FindControl("lblTest").Visible = true;
        }
    }
}

在Listview中,您可以執行以下操作,設置事件並在占位符上添加控件

<asp:ListView ID="ListViewTest" DataSourceID="..." OnItemDataBound="ListViewTest_ItemDataBound" runat="server">
    <LayoutTemplate>
      <table>
        <tr>
            <th>Column Name</th>
        </tr>
        <tr runat="server" id="itemPlaceholder" />
      </table>          
    </LayoutTemplate>
    <ItemTemplate>
      <tr style="background-color: #CAEEFF" runat="server">
        <td>
           <%-- both controls are here --%>     
          <asp:Button ID="btnTest" runat="server" Visible="false" Text="Like"></asp:Button>
          <asp:Label ID="lblTest"  runat="server" Visible="false" Text="hello"></asp:Label>
        </td>
      </tr>
    </ItemTemplate>
  </asp:ListView>

您確定不是這行: <%# test= Eval("testId")%>向輸出寫入true或false嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM