当列数据具有特定值时如何阻止 GridView 中的命令按钮?
How to block command button in GridView when column data has specific value?
我目前有一个包含以下列的 GridView
| # | Date | Line | Process | Equipment | Step | Status | |
---------------------------------------------------------------------
| 1 | 9/10/2020 | A1 | ABCD | SXCD | Test | Open | Edit |
最后一列每行都有一个命令按钮。
'ASP for button column for reference
<asp:TemplateField>
<ItemTemplate>
<asp:linkbutton ID="Edit" runat="server" onclick="edit_click"
commandbutton = "MySelect" commandargument = "<%# container.displayindex %>"
text="Edit"></asp:linkbutton>
</ItemTemplate>
</asp:TemplateField>
我现在计划在状态显示 Close
时阻止它的功能。
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim StatusLog As String = DataBinder.Eval(e.Row.DataItem, "Status").ToString()
If StatusLog = "Close" Then
Dim lb As LinkButton = DirectCast(e.Row.Cells(8).Controls(0), LinkButton)
lb.Visible = False
End If
End If
End Sub
这是我获取的代码。测试的时候发现Edit按钮即使Status为Close
.
依然可以交互
我在这里错过了什么?
嗯,该代码应该可以工作。
但是,作为一般规则,您可能没有正确选择控件。
我建议此代码具有强类型控件,并使用查找控件。
冒细胞的风险太难了,而且总是可以有某种额外的控制或分隔符等。我发现最好使用 FindControl。
所以,这样说:
If e.Row.RowType = DataControlRowType.DataRow Then
Dim StatusLog As String = DataBinder.Eval(e.Row.DataItem, "Status").ToString()
If StatusLog = "Close" Then
Dim lb As LinkButton = e.Row.FindControl("Edit")
lb.Visible = False
End If
End If
我目前有一个包含以下列的 GridView
| # | Date | Line | Process | Equipment | Step | Status | |
---------------------------------------------------------------------
| 1 | 9/10/2020 | A1 | ABCD | SXCD | Test | Open | Edit |
最后一列每行都有一个命令按钮。
'ASP for button column for reference
<asp:TemplateField>
<ItemTemplate>
<asp:linkbutton ID="Edit" runat="server" onclick="edit_click"
commandbutton = "MySelect" commandargument = "<%# container.displayindex %>"
text="Edit"></asp:linkbutton>
</ItemTemplate>
</asp:TemplateField>
我现在计划在状态显示 Close
时阻止它的功能。
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim StatusLog As String = DataBinder.Eval(e.Row.DataItem, "Status").ToString()
If StatusLog = "Close" Then
Dim lb As LinkButton = DirectCast(e.Row.Cells(8).Controls(0), LinkButton)
lb.Visible = False
End If
End If
End Sub
这是我获取的代码。测试的时候发现Edit按钮即使Status为Close
.
我在这里错过了什么?
嗯,该代码应该可以工作。 但是,作为一般规则,您可能没有正确选择控件。
我建议此代码具有强类型控件,并使用查找控件。 冒细胞的风险太难了,而且总是可以有某种额外的控制或分隔符等。我发现最好使用 FindControl。
所以,这样说:
If e.Row.RowType = DataControlRowType.DataRow Then
Dim StatusLog As String = DataBinder.Eval(e.Row.DataItem, "Status").ToString()
If StatusLog = "Close" Then
Dim lb As LinkButton = e.Row.FindControl("Edit")
lb.Visible = False
End If
End If