开发者

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

开发者 https://www.devze.com 2022-12-10 15:48 出处:网络
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 separat开发者_高级运维e class as a static method, since this code may be call

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 separat开发者_高级运维e 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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消