DKR DKR - 1 month ago 9
ASP.NET (C#) Question

How to trigger c# method from anchor tag in asp.net

I have an anchor tag in .aspx page as below:

<a href="#divrecentQ" runat="server" id="linkdivrecentQ" onclick="alert()" onserverclick="divrecentQInit" aria-controls="divrecentQ" role="tab" data-toggle="tab">Click</a>


I want to tigger c# method using this tag.

protected void divrecentQInit(object sender, EventArgs e)
{
log.Debug("divrecentQInit is called");

}


Problem is: divrecentQInit method is not getting called.

Thanks for help.

Not: I do not want to use LinkButton of asp.net since it doesnt work very well with bootstrap.

Answer

The Method call event(onServerClick) is case-sensitive, Use

onServerClick="divrecentQInit"

instead of

onserverclick="divrecentQInit"

and additionally I would suggest using <asp:LinkButton to achieve the same(you would not need onServerClick in this case, OnClick instead )

<asp:LinkButton runat="server" OnClick="divrecentQInit" />