LoginView button issue

I'm using a login view control that will show a button if the users role is "admin".   This button has an onClick event in the code behind page.

If the user is NOT and admin the button doesn't load.  That's how I want it to function.  Admins get the button, users do not.

Here's my problem.

If a user who is not an admin visits the page the page throws a Compilation error that points to the onClick event.  I guessing it's trying to tell me that there is no button associated with this event.

How do I fix this?

cdemott33Asked:
Who is Participating?
 
David RobitailleAnalyst ProgrammerCommented:
more easy, remove the "Handles btnLogout.Click" and add onclick="btnLogout_Click" to btnLogout (in the aspx page)  
0
 
cdemott33Author Commented:
Here's my code, if you need it.
<!-- CODE FOUND IN USER CONTROL -->
 
    <asp:LoginView ID="LoginView1" runat="server">
        <RoleGroups>
            <asp:RoleGroup Roles="administrator">
            <ContentTemplate>
                <div id="userControls">
                   <asp:LinkButton ID="btnLogout" runat="server">Logout</asp:LinkButton>  
                </div>
            </ContentTemplate>
            </asp:RoleGroup>
        </RoleGroups>
    </asp:LoginView>
 
<!-- MY USER CONTROL CODE BEHIND BUTTON CLICK EVENT -->
 
Imports System.Web.Security
 
Partial Class pm_header
    Inherits System.Web.UI.UserControl
 
    Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogout.Click
        Session.Abandon()
        Session.Contents.RemoveAll()
        FormsAuthentication.SignOut()
        Response.Cookies.Remove(FormsAuthentication.FormsCookieName)
        FormsAuthentication.RedirectToLoginPage()
    End Sub
End Class

Open in new window

0
 
cdemott33Author Commented:
And this is the Compliler Error.  FYI: Line 6 below is highlighted in red.

Compiler Error Message: BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

Source Error:

 

Line 4:      Inherits System.Web.UI.UserControl
Line 5:  
Line 6:      Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogout.Click
Line 7:          Session.Abandon()
Line 8:          Session.Contents.RemoveAll()
 
0
 
cdemott33Author Commented:
That worked perfectly!  Thank you davrob60
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.