Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Hiding controls defined within templates to some users

Total Hit ( 2630)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


It happens frequently that you want to hide some input controls or links if the current user is not logged in (it runs with the anonymous account), or if the logged user is not a member of a specific group. For example, you may want to hide the Edit/Delete buttons to users that are not Administrators. For single controls on the page, you just programmatically set the Visible property according to your rules. However, this may seem more difficult to do if the Edit/Delete controls are defined within a template repeated for each data item bound to the parent control (a Repeater, DataList or DataGrid), because you don't have direct access to the single instances of the controls. However, the solution is very simple: you just bound the Visible property to an expression that's evaluated at runtime, when you call the parent control's DataBind method. Say for example that you want to hide a button for the anonymous user - you just have to check that User.Identity.IsAuthenticated is True, and use this value for the button's Visible property, as shown below:

Click here to copy the following block
<asp:DataList Runat="server" ...>
  <ItemTemplate>
    <asp:LinkButton runat="server" Text="Edit" CommandName="Edit"
      Visible='<%# User.Identity.IsAuthenticated %>'/>
    <!-- other controls for the template -->
  </ItemTemplate>
</asp:DataList>

In more complex scenarios, you can bind the Visible property to a Protected/Public function defined in the code-behind, that checks whether the user is authenticated and its parent group, and returns True or False accordingly. Here's the example:

Click here to copy the following block
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" Visible='<%# ShowAdminControls() %>'/>

In the code-behind file:

Click here to copy the following block
Protected Functions ShowAdminControls() As Boolean
  If User.IsInRole("BUILTIN\Administrators") AndAlso User.Identity.Name = _
    "SomeName" Then
    Return True
  End If
End Function



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.