0

javascriptを使用すると、asp.netグリッドビューで、表示されているboolen値が次のように文字列に切り替わります。

<asp:Label ID="Correct" Text='<%# Eval("Correct").ToString().Equals("true") ? " Correct " : " Wrong " %>'  runat="server"/></td>

データ型がintで、値が1、2、3の場合、それぞれ低、中、高を表示するために3つの異なる値を切り替える方法はありますか。私は次のように試しましたが、機能しません

<asp:Label ID="Difficulty" Text='<%# Eval("Difficulty").ToString().Equals("1") ? " low" : (("Difficulty").ToString().Equals("2") ? " medium " : " high ") %>'  runat="server"/></td>
4

1 に答える 1

1

これが三項演算子で実行できるとしても、読みにくいです。あなたの最善の策は、クラスの背後にある対応するコードで関数を定義することだと思います。

protected string GetDifficultyText(object difficultyObj)
{
    string difficultyId = difficultyObj as string;

    if (string.IsNullOrWhiteSpace(difficultyId))
    {
        return string.Empty; //or throw exception
    }

    switch (difficultyId)
    {
        case "1":
            return " low";
        case "2":
            return " medium";
        case "3":
            return " high";
        default:
            return string.Empty; //or throw exception
    }
}

そして、それをマークアップで呼び出します。

<asp:Label ID="Difficulty"
           Text='<%# GetDifficultyText(Eval("Difficulty")) %>'
           runat="server"/>
于 2012-09-21T14:17:38.040 に答える