views:

209

answers:

2

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?

+2  A: 

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

o.k.w
It's not in the codebehind though, it's in a seperate class, so how would it find it?
SLC
@SLC: Ok, I've amended my answer
o.k.w
I was hoping to avoid this and link it directly, but if there is no other way, I'll do it like this, cheers.
SLC
@SLC: You might be able to do it by wiring up the event-handler for button click, but the wiring have to be done in the code-behind anyhow. So well.... the only alternative might be to create a custom linkbutton and implement your own click handler. Doubt so huh?
o.k.w
Simple is best, the simplest solution is what I tried, the 2nd simplest is the one you posted, so that's the one I'll use ;)
SLC
A: 

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

Kieron
@Kieron: Both you and I misunderstood his question. The function to be triggered is outside the scope of the page. :)
o.k.w