簡體   English   中英

ASP.net數據綁定字段(C#)中的條件運算符

[英]Conditional Operator in ASP.net Databound Field (C#)

我正在嘗試在DataList的數據綁定字段中實現條件格式,但錯誤:正在生成Invalid expression term 'if' 我的代碼如下:

<asp:DataList ID="dlItems" runat="server">
    <ItemTemplate>
        <asp:Label ID="lblDescription" runat="server" Text='
            <%# if (Eval("Description").ToString().Length <= 150)
            // The error is generated by the 'if' on the above line
                    Eval("Description");
                else
                    Eval("Description").ToString().PadRight(150).Substring(0,150).TrimEnd(); %>'>
        </asp:Label>
    </ItemTemplate>
</asp:DataList>

注意: else語句中的代碼基本無關; 即使被排除,我也會遇到相同的錯誤。

您可以使用以下內容

<asp:DataList ID="dlItems" runat="server">
    <ItemTemplate>
        <asp:Label ID="lblDescription" runat="server" Text='
            <%# Eval("Description").ToString().Length <= 150?Eval("Description"):          
                    Eval("Description").ToString().PadRight(150).Substring(0,150).TrimEnd() %>'>
        </asp:Label>
    </ItemTemplate>
</asp:DataList>

希望對你有幫助

暫無
暫無

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

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