how to hide Edit button in detailsView programmatically?

Last post 05-08-2008 2:34 PM by lberan. 6 replies.

Sort Posts:

  • how to hide Edit button in detailsView programmatically?

    05-08-2008, 12:41 PM
    • Loading...
    • lberan
    • Joined on 02-12-2008, 7:23 PM
    • Pittsburgh, PA
    • Posts 271

    I want to hide Edit button on detailsView if a user is not authorized to edit so that the user can only view the details. 

    Lynn

    Please remember to click "Mark as Answer" on the post to all the replies that have helped you so that these answers could help others. Thanks.
  • Re: how to hide Edit button in detailsView programmatically?

    05-08-2008, 1:07 PM
    Answer
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 5,800
    • Moderator
      TrustedFriends-MVPs

    One option is to display your Edit Button within a TemplateField and call a small helper function in the code-behind like so:

    ASPX 

    <asp:linkbutton id="btnEdit" runat="server" commandname="Edit" text="Edit" visible='<%# AuthorizedToEdit() %>' />

    CODE-BEHIND 

    protected bool AuthorizedToEdit()
    {
    	return // some boolean expression here to indicate permission to edit
    }
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: how to hide Edit button in detailsView programmatically?

    05-08-2008, 1:53 PM
    • Loading...
    • lberan
    • Joined on 02-12-2008, 7:23 PM
    • Pittsburgh, PA
    • Posts 271

    thanks, Ed.  Could you please modify the following code for me?

    protected sub bool AuthorizedToEdit() 'the error says End of Statement Expected

    Return    booleanExpression ' the error says Return statement in a sub cannot return a value

    End Sub

    Lynn

    Please remember to click "Mark as Answer" on the post to all the replies that have helped you so that these answers could help others. Thanks.
  • Re: how to hide Edit button in detailsView programmatically?

    05-08-2008, 2:13 PM
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 5,800
    • Moderator
      TrustedFriends-MVPs

    This is hard to do since I have no idea how your application is configured for security. Most often, this would simply be a call to see if the currently logged-in user was in a particular role or not. Something like this:

    return Roles.IsUserInRole(this.User.Identity.Name, "Edit");

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: how to hide Edit button in detailsView programmatically?

    05-08-2008, 2:17 PM
    • Loading...
    • lberan
    • Joined on 02-12-2008, 7:23 PM
    • Pittsburgh, PA
    • Posts 271

    my problem is not how to write the boolean expression but how to convert your code to VB.net

    please read the error i got and reply if you can. 

    thanks.

    Lynn

    Please remember to click "Mark as Answer" on the post to all the replies that have helped you so that these answers could help others. Thanks.
  • Re: how to hide Edit button in detailsView programmatically?

    05-08-2008, 2:27 PM
    Answer
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 5,800
    • Moderator
      TrustedFriends-MVPs

    Use a Function as oposed to a Sub: 

    Protected Function AuthorizedToEdit() As Boolean
    	Return True
    End Function
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

    protected void Post_Answered(object sender, EventArgs e) { if (this.MarkAsAnswered != null) { this.MarkAsAnswered(this, EventArgs.Empty); } }
  • Re: how to hide Edit button in detailsView programmatically?

    05-08-2008, 2:34 PM
    Answer
    • Loading...
    • lberan
    • Joined on 02-12-2008, 7:23 PM
    • Pittsburgh, PA
    • Posts 271

    thanks. Ed.

    Methods can be declared as a Sub or a Function. A Sub is a method that doesn't return anything and a Function is a method that does return something.

    Lynn

    Please remember to click "Mark as Answer" on the post to all the replies that have helped you so that these answers could help others. Thanks.
Page 1 of 1 (7 items)