[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Hid a GridView Template Control

Posted on 2010-09-11
6
Medium Priority
?
642 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 2000 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

650 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