?
Solved

Disable a Hyperlink in a gridview

Posted on 2010-09-10
3
Medium Priority
?
446 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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