Solved

Disable a Hyperlink in a gridview

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now