Solved

Hid a GridView Template Control

Posted on 2010-09-11
6
638 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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