Yasserzaid’s Weblog

September 16, 2009

Disable Edit and Insert and New Buttons in DetailsView according to Roles

Filed under: ASP.Net — yasserzaid @ 10:57 pm

Hi

try this example :-

<asp:TemplateField ShowHeader=”False”>
    <ItemTemplate>
         <asp:LinkButton ID=”LinkButton1″ runat=”server” CausesValidation=”False” Visible=’<%# GetShowEditButton() %>’
                             CommandName=”Edit” Text=”Edit”></asp:LinkButton>
         &nbsp;<asp:LinkButton ID=”LinkButton2″ runat=”server” CausesValidation=”False” Visible=’<%# GetShowInsertButton() %>’
                             CommandName=”New” Text=”New”></asp:LinkButton>
         &nbsp;<asp:LinkButton ID=”LinkButton3″ runat=”server” CausesValidation=”False”
                             CommandName=”Delete” Text=”Delete” Visible=’<%# GetShowDeleteButton() %>’></asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

in your codebehind, you have the following:

    protected bool GetShowEditButton()
    {
        return Roles.IsUserInRole(“Admin”);
    }
    protected bool GetShowInsertButton()
    {
        return Roles.IsUserInRole(“Admin”);
    }
    protected bool GetShowDeleteButton()
    {
        return Roles.IsUserInRole(“Admin”);
    }

Hope this helps

Good Luck

4 Comments »

  1. Thank you! It works on my project.

    Comment by Judy — November 4, 2009 @ 3:27 pm

  2. can you convert the code to vb.net? thank you

    Comment by john — November 9, 2009 @ 4:50 am

  3. Thank you Judy hope this helps you
    Good Luck

    Comment by yasserzaid — November 9, 2009 @ 11:33 am

  4. Hi John
    try this using vb.net

    Protected Function GetShowEditButton() As Boolean
    Return Roles.IsUserInRole(Admin)
    End Function
    Protected Function GetShowInsertButton() As Boolean
    Return Roles.IsUserInRole(Admin)
    End Function
    Protected Function GetShowDeleteButton() As Boolean
    Return Roles.IsUserInRole(Admin)
    End Function

    Good Luck.

    Comment by yasserzaid — November 9, 2009 @ 11:34 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.