Search code examples
c#asp.netlinkbutton

ASP.Net LinkButton click event gives error


I have a linkbutton in a gridview control in my asp.net app that generates an error upon page_load ("subscribersearch.aspx"). How can I resolve this error?

HTML Code is:

<ItemTemplate>
        <asp:LinkButton OnClick="LinkButton_Click" id="lbtnViewSubscriberHistory" CommandName="ViewSubscriberHistory" Visible="true" runat="server" ToolTip="Click to view History." >Subscriber History</asp:LinkButton>
</ItemTemplate> 

CodeBehind:

void LinkButton_Click(Object sender, EventArgs e)
{
    Alert("Test", "You clicked the link button");
}

Error message:

System.Web.HttpException (0x80004005): Error executing child request for SubscriberSearch.aspx. --->

System.Web.HttpCompileException (0x80004005): l:\CorpApp\Web\us\SubscriberSearch.aspx(365):
error CS1061: 'ASP.subscribersearch_aspx' does not contain a definition for 'LinkButton_Click' and no extension method 'LinkButton_Click' accepting a first argument of type 'ASP.subscribersearch_aspx' could be found (are you missing a using directive or an assembly reference?)


Solution

  • Add protected before the method

    protected void LinkButton_Click(Object sender, EventArgs e)
    {
        Alert("Test", "You clicked the link button");
    }