Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Disable a Hyperlink in a gridview

Posted on 2010-09-10
3
Medium Priority
?
452 Views
Last Modified: 2012-05-10
Hello All,
How can I disable <asp:HyperLinkField control in a gridview based on a session["role"].
Thanks
0
Comment
Question by:Star79
[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
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
MlandaT earned 2000 total points
ID: 33648575
Option 1:
<asp:TemplateField HeaderText="Link">
    <ItemTemplate>
      <asp:HyperLink ID="myHyperLinkControl" Runat="Server" Enabled='<%# (session["role"] = "Admin") %>' />
    </ItemTemplate>
</asp:TemplateField>  

Option 2:
1. Convert the column into a templatecolumn (I assume here that you eventually give the hyperlink control an ID of 'myHyperLinkControl')
2. Use the RowDataBound event
3. In rowdatabound event, find the control and do what you want with it

  Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then
   
         dim myLink as HyperLink = e.Row.FindControl("myHyperLinkControl")
        if myLink isnot nothing then
             myLink.Enabled = (session["role"] = "Admin")
        end if
       
    End If
   
  End Sub
0
 

Author Comment

by:Star79
ID: 33648773
Hello,
Without converting into template column can we do this?
Thanks
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 33648852
In the RowDataBound event above... you can just use this syntax:

dim myLink as HyperLink = e.Row.Cells(indexOfTheHyperLinkColumn).Controls(0)
if myLink isnot nothing then
     myLink.Enabled = (session["role"] = "Admin")
end if

NOTES:
indexOfTheHyperLinkColumn based on the position of the column in the gridview. First index is 0.
Please just check whether the HyperLink control is Controls(0) or otherwise Controls(1)... dont remember which is which
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 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