[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 643
  • Last Modified:

Hid a GridView Template Control

I have a gridview ("Grid_Projects") that has a template field (See Code Below). I want to hide the LinkButton ("LinkButton61") from some users using the VB code behind the page. Sounds easy, but I can't figure out the proper syntax to access the control.

I've tried a bunch of things like:

Dim nn As System.Web.UI.WebControls.LinkButton = _
Grid_Projects.TemplateControl.FindControl("LinkButton61")

nn.Visible = False

but I can't seem to get a valid reference to the control to make it not visible.

I'm working in VB.NET in VS2010, so please make your responses appropriate to this environment.


<asp:TemplateField ShowHeader="False">
        <EditItemTemplate>
          <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
            CommandName="Update" Text="Update"></asp:LinkButton>
          &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
            CommandName="Cancel" Text="Cancel"></asp:LinkButton>
        </EditItemTemplate>
        <FooterTemplate>
          <asp:LinkButton ID="Project" runat="server">Add Project</asp:LinkButton>
        </FooterTemplate>
        <ItemTemplate>
          <asp:LinkButton ID="LinkButton61" runat="server" CausesValidation="False" 
            CommandName="Edit" Text="Edit"></asp:LinkButton>
          &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
            CommandName="Select" Text="Select"></asp:LinkButton>
        </ItemTemplate>
      </asp:TemplateField>

Open in new window

0
lippert
Asked:
lippert
  • 3
  • 2
1 Solution
 
calypsoworldCommented:
Hi!

You could resolve this way:

In webpage:

<asp:LinkButton ID="LinkButton61" runat="server" CausesValidation="False"
            CommandName="Edit" Text="Edit" Visible='<%# defineInvisible() %>' ></asp:LinkButton>

In code-behind (C#):

public String defineInvisible() {
  return "false";
}


I hope it helps you.
Regards.

0
 
lippertAuthor Commented:
Calypsoworld, thanks. This would probably work in this specific case, but I'm looking for a more generic solution. I really want to know the syntax required to get a valid reference to the control.  
0
 
guru_samiCommented:
use GridView RowDataBound event something like below:
Sub Grid_Projects_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles Grid_Projects.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
                Dim btn As LinkButton = DirectCast(e.Row.FindControl("LinkButton61"), LinkButton)
    End If
  End Sub
Ref: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
Or you can loop through GridView rows, if your requirements are different.
But in any case you will have to find control in GridView Row and not Gridview.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
lippertAuthor Commented:
guru_sami, I think you might have mis-read the code block. The LinkButton control is not a databound control. It is a command field converted to a template field. If I can simply set the visible attribute in the ASPX page to false (as in calypsoworld's response) why can't I get a reference to the control in the VB code page then set the attribute to false there? I don't understand?
0
 
guru_samiCommented:
LinkButton is inside of the GridView ItemTemplate and so you cannot access it directly like a normal control.
The code I provided is for GridView RowDataBound event which is fired when each gridview row is databound. So you find your LinkButton for each row and then set it visible or hidden as per your requirement.
 If e.Row.RowType = DataControlRowType.DataRow Then
                Dim btn As LinkButton = DirectCast(e.Row.FindControl("LinkButton61"), LinkButton)
                If btn IsNot Nothing Then
                        btn.Visible = true /false as per your need
                End If
    End If
0
 
lippertAuthor Commented:
Thanks, that works.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now