Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在运行时设置ItemTemplate属性_C#_Asp.net_Gridview - Fatal编程技术网

C# 在运行时设置ItemTemplate属性

C# 在运行时设置ItemTemplate属性,c#,asp.net,gridview,C#,Asp.net,Gridview,我试图在ASP.Net应用程序的GridView中显示一个小进度条。我正在尝试使用ItemTemplate来完成它 <ItemTemplate> <table width="100%"> <tr>

我试图在ASP.Net应用程序的GridView中显示一个小进度条。我正在尝试使用ItemTemplate来完成它

                            <ItemTemplate>
                                <table width="100%">
                                    <tr>
                                        <td style="width: 75%; background-color: red"></td>
                                        <td style="width: 25%; background-color: green" ></td>
                                    </tr>
                                </table>
                            </ItemTemplate>

我想根据行中某些值的计算设置宽度百分比


用Eval可以做到吗?或者我需要做一些代码隐藏吗?

您可以尝试以下方法(下面是示例计算):

并使用相同的数据绑定语法调用它:

<td style='<%# GetCellStyle((int)Eval("Width")) %>'></td>


谢谢。代码隐藏可能更易于维护/调试。将使用什么事件?在行?@Craig,请查看更新。您也可以使用
RowDataBound
事件,但这似乎是一项开销。谢谢Andrei。这看起来是一个很好的方法。我现在唯一的问题是,我不能100%确定如何从用于生成行的ObjectDataSource行中获取所需的字段。我的数据行中有两个字段,用于创建此行:initialProductiveHours和ProductiveHoursLeving,我想从中显示剩余时间的百分比。@Craig,刚才的
Eval(“initialProductiveHours”)
Eval(“ProductiveHoursLeving”)
有问题吗?让上面的函数接受两个参数,执行计算,您应该会很好。谢谢!工作完全符合预期。
protected string GetCellStyle(int width)
{
    return string.Format("width: {0}%; background-color: red", width / 100);
}
<td style='<%# GetCellStyle((int)Eval("Width")) %>'></td>