Check Control是LinkBut​​ton?

时间:2013-11-18 13:54:07

标签: asp.net gridview

如何检查此控件是否为linkBut​​ton?

CType(e.Row.Cells(0).Controls(0),LinkBut​​ton)

这是在网格视图行数据绑定内。

4 个答案:

答案 0 :(得分:3)

如果您使用TemplateFields,则应使用FindControl获取对您控件的引用:

LinkButton myLinkButton = (LinkButton) e.Row.FindControl("LinkButtonID");

如何检查类型,请回答您的问题:

Type Checking: typeof, GetType, or is?

另一个是使用as运算符:

LinkButton myLinkButton = e.Row.Cells(0).Controls(0) as LinkButton;
if(myLinkButton != null); // successfull cast

修改,因为DataControlLinkButton辅助功能为Friend,您可以直接使用它(除了您自己的GetType().ToString方法)。但是因为它继承自LinkButton,你可以检查:

通过Is

If TypeOf control Is LinkButton Then
    DirectCast(control, LinkButton).Visible = False
End If

通过TryCast(C#作为运营商):

Dim lbEdit = TryCast(e.Row.Cells(0).Controls(0), LinkButton)
If lbEdit IsNot Nothing Then
    lbEdit.Visible = False
End If

通过GetType

If control.GetType() = GetType(LinkButton) Then
    DirectCast(control, LinkButton).Visible = False
End If

答案 1 :(得分:1)

 If e.Row.Cells(0).Controls(0).GetType().ToString = "System.Web.UI.WebControls.DataControlLinkButton" Then
   Dim lbEdit As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)
   lbEdit.Visible = False
End If

答案 2 :(得分:0)

if((e.Row.Cells(0).Controls(0)) is LinkButton)
{ 
   ((LinkButton)e.Row.Cells(0).Controls(0)).visible = false;
}

答案 3 :(得分:0)

 Dim lnkbtn As LinkButton = CType(e.Row.Findcontrol("lnkbuttonname"), LinkButton)

Protected Sub dgrd_WWWH_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles dgrd_WWWH.RowCommand

Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim txtwwwhid = CType(row.FindControl("txtwwwhid"), Label)
Dim txtwho = CType(row.FindControl("txtWho"), LinkButton)
Dim txtwho1 = CType(row.FindControl("txtWho1"), LinkButton)

End Sub