基于绑定数据的GridView样式
我希望 GridView 的行基于名为 IsObsolete 的绑定数据值添加删除线。我尝试这样做:
<RowStyle BackColor="#EFF3FB" Font-Strikeout='<%# Bind('IsObsolete') %>' />
但显然这无法解析。我不想在 GridView.DataBound() 中执行此操作。还有其他想法吗?
I would like the rows of my GridView to have strikethrough based on a bound data value called IsObsolete. I tried to do this:
<RowStyle BackColor="#EFF3FB" Font-Strikeout='<%# Bind('IsObsolete') %>' />
But obviously this doesn't parse. I'd rather not do this in GridView.DataBound(). Any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过在模板中一个控件的
DataBinding
事件上应用一种样式来实现此目的。示例:然后实现
OnDataBinding
事件:这只是一个快速示例,其中包含一个带有链接的列,该链接也会根据数据绑定进行修改,但如果对其进行调整,您应该能够了解要点以满足您的需求。我喜欢在数据绑定上执行此操作,因为我在 aspx 代码中不进行内联绑定。
I do this by applying a style on the
DataBinding
event of one of my controls in a template. Example:Then implement the
OnDataBinding
event:This is just an quick example with a column with a link that gets modified based on the databinding as well but you should be able to get the gist of if an tweak it to suit your needs. I like doing it on the databinding because I do no binding inline in my aspx code.
由于 RowStyle 元素适用于整个网格,因此完成您想要的操作的唯一方法是为所有列设置 TemplateItems,并根据相同的数据值将 CssClass 应用到每列。
我不确定您避免执行此操作的 DataBound 事件的原因,因为这将是完成它的最简单方法。
您也可以尝试使用格式化函数和项目样式。从上面窃取一些代码并更改它:
Since the RowStyle element is applicable to the entire grid, the only way to accomplish what you want would be to have TemplateItems set for all columns and apply a CssClass to each column based on that same data value.
I'm not sure for your reasoning for avoiding the DataBound event for doing this as that would be the simplest way to accomplish it.
You might also try using a formatting function and itemstyles. Stealing a tidbit of code from above and changing it: