1

How to check if this control is linkButton?

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

This is inside grid view row data bound.

4 Answers4

3

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
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Its not TemplateFields its the Edit Delete Buttons when we enable AutoGenerateDeleteButton & AutoGenerateEditButton. – MohammadMMohammad Nov 18 '13 at 13:56
  • @MohammadMMohammad:Why do you need it in `RowDataBound`? – Tim Schmelter Nov 18 '13 at 14:00
  • My problem is when I'm exporting to excel I'm hiding these links but its catching error on this line since the first column in no longer a linkbutton. that is why I want to check if they are linkbuttons before assigning them orelse it will catch errors – MohammadMMohammad Nov 18 '13 at 14:00
  • @MohammadMMohammad: Why don't you hide the complete column(s)? `gridView1.Columns[0].Visible=false;` – Tim Schmelter Nov 18 '13 at 14:01
  • This don't work with AutoGenerateDeleteButton & AutoGenerateEditButton as they are built it columns and not templateFields. When I did this it hide my first templateField I have in the grid... – MohammadMMohammad Nov 18 '13 at 14:10
  • For LinkButton myLinkButton = e.Row.Cells(0).Controls(0) as LinkButton; If the first column is not LinkButton in case I hide them its catching error that is why I want to check their type to check if they are LinkButton to proceed. was I clear for you with my problem here? – MohammadMMohammad Nov 18 '13 at 14:12
  • I did the checking, check my solution – MohammadMMohammad Nov 18 '13 at 14:32
  • don't forget to vote me up if you like my solution it works for me. – MohammadMMohammad Nov 18 '13 at 14:36
  • @MohammadMMohammad: I've edited my answer also to show you how you can check the types without using it's string representation (upvoted your answer anyway). – Tim Schmelter Nov 18 '13 at 15:15
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
0
if((e.Row.Cells(0).Controls(0)) is LinkButton)
{ 
   ((LinkButton)e.Row.Cells(0).Controls(0)).visible = false;
}
Akshay
  • 11
  • 1
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
senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36