【发布时间】:2020-02-15 09:57:58
【问题描述】:
我有一个状态列,其中包括已批准、已拒绝或已取消。我有另一个包含链接按钮批准和拒绝的操作列。现在,如果状态为 Cancelled,我希望禁用该行的链接按钮。
我尝试使用 GridView1_DataBound 事件但无法弄清楚逻辑。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Server.HtmlDecode(e.Row.Cells[0].Text.Trim()).Equals("Cancelled"))
{
//OR you can disable it instead of complately hiding it
((LinkButton)GridView1.Rows[e.Row.RowIndex].Cells[0].Controls[0]).Enabled = false;
}
}
}
【问题讨论】:
-
所以你说上面的代码不起作用,对吗?你做了哪些调试来尝试确定它开始出错的地方?
-
P.S.在一个地方你使用
e.Row.Cells来识别行,而在另一个地方你(似乎是随机的)使用更长的GridView1.Rows[e.Row.RowIndex].Cells来做同样的工作。为什么要让它变得更复杂?通过简单的方法使您的代码保持一致、更短、更易于阅读。 -
GridView 中使用了什么样的对象?就像,如果我的 GridView.DataSource = Collection(Of Products),那么在 RowDataBound 上我正在处理 1 个单独的产品。所以我会将该行转换为产品对象,然后我可以轻松访问产品属性以查看它是否已被取消。
-
@JohnPete22 好主意。但我假设您的意思是您将投射行的DataItem 而不是行本身? :-)
-
@ADyson 是的,这是正确的。抱歉,有时我忘记了我需要更加具体。例如
Product item = (Product)e.Row.DataItem
标签: c# asp.net gridview webforms