Solved

Disable a Hyperlink in a gridview

Posted on 2010-09-10
3
442 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 30

Accepted Solution

by:
MlandaT earned 500 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 30

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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