Solved

Hid a GridView Template Control

Posted on 2010-09-11
6
637 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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