Solved

Disable a Hyperlink in a gridview

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

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

738 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