NibblyPig
NibblyPig

Reputation: 52952

ASP.NET button says it can't find the method I put in OnClick

I have a log out button on my main menu and I want it to run a method to log out. However I want to store this method in a separate class as a static method, since this code may be called from other locations too.

Compiler error message:

CS1061: 'ASP.adminpages_masterpages_adminsystem_master' does not contain a definition for 'ExtensionMethods' and no extension method 'ExtensionMethods' accepting a first argument of type 'ASP.adminpages_masterpages_adminsystem_master' could be found (are you missing a using directive or an assembly reference?)

My ExtensionMethods class:

namespace ExtensionMethods
{
    public static class MyExtensionMethods
    {
        public static void Logout(object sender, EventArgs e)
        {
               //Logout Code
        }
    }
}

My button:

<asp:LinkButton runat="server" OnClick="ExtensionMethods.MyExtensionMethods.Logout" Text="Log Out"></asp:LinkButton>

Ideas?

Upvotes: 0

Views: 2669

Answers (2)

o.k.w
o.k.w

Reputation: 25820

You need to handle the button-click at the code-behind and let that call your static method in other class.

    <asp:LinkButton id="button1" runat="server" 
       OnClick="LinkButton_Click" Text="Log Out"></asp:LinkButton>

Code-behind:

   void LinkButton_Click(Object sender, EventArgs e) 
   {
      ExtensionMethods.MyExtensionMethods.Logout(sender, e);
   }

Check the reference for more examples: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.click.aspx

Upvotes: 2

Kieron
Kieron

Reputation: 27127

Your event handler should really be in the page class and not be static. It should be, ideally, protected but it can be public.

Upvotes: 0

Related Questions