MohammadMMohammad
MohammadMMohammad

Reputation: 305

Check Control is LinkButton?

How to check if this control is linkButton?

CType(e.Row.Cells(0).Controls(0), LinkButton)

This is inside grid view row data bound.

Upvotes: 1

Views: 3736

Answers (4)

senthilkumar2185
senthilkumar2185

Reputation: 2568

 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

Upvotes: 0

Akshay
Akshay

Reputation: 11

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

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460158

If you use TemplateFields you should use FindControl to get the reference to your control:

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

To answer your question how to check the type:

Type Checking: typeof, GetType, or is?

Another one is using as operator:

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

Edit since DataControlLinkButton accessibility is Friend you canot use it directly(apart from your own GetType().ToString approach). But because it inherits from LinkButton you can check that:

Via Is:

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

Via TryCast(C# as operator):

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

Via GetType:

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

Upvotes: 3

MohammadMMohammad
MohammadMMohammad

Reputation: 305

 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

Upvotes: 1

Related Questions