0

使用 javascript,在 asp.net 网格视图中,将显示的布尔值切换为字符串,如下所示

<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 回答