Solved

Hid a GridView Template Control

Posted on 2010-09-11
6
633 Views
Last Modified: 2012-05-10
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
Comment
Question by:lippert
  • 3
  • 2
6 Comments
 

Expert Comment

by:calypsoworld
ID: 33655378
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
 
LVL 1

Author Comment

by:lippert
ID: 33656695
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
 
LVL 41

Expert Comment

by:guru_sami
ID: 33657190
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:lippert
ID: 33659216
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
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 33659373
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
 
LVL 1

Author Closing Comment

by:lippert
ID: 33659496
Thanks, that works.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now