c# html hyperlink call a method

17,111
<a ID="MyAnchor"
    OnServerClick="menuPersonTab_OnMenuItemClick" 
    runat="server"> Click This </a>

Here is an explained example from MSDN.

Share:
17,111
gdubs
Author by

gdubs

Updated on June 04, 2022

Comments

  • gdubs
    gdubs almost 2 years

    is it possible to call a method using a regular hyperlink like so:

    <div class="menu">
    <ul>
    <li><a href=#> click this </a></li>
    <li><a href=#> click this again </a></li>
    </ul>
    <div>
    

    call this method:

    protected void menuPersonTab_OnMenuItemClick(object sender, MenuEventArgs e)
    {
        mvPerson.ActiveViewIndex = Int32.Parse(menuPersonTab.SelectedValue);
    }
    

    no? yes?

  • gdubs
    gdubs about 13 years
    it gives me a > Compiler Error Message: CS1026: ) expected
  • gdubs
    gdubs about 13 years
    this gives me a CS1501: No overload for method 'menuPersonTab_OnMenuItemClick' takes 0 arguments
  • yogsma
    yogsma about 13 years
    Did you try using = instead of #?
  • Anton
    Anton about 13 years
    If you are not able to attach server side event to code(for example - HTML code is creating by Javascript) but you need to call server event - you can call javascript function __doPostBack with needed parameters. __doPostBack will post back data to server where you can process it and call necessary function.
  • Anton
    Anton about 13 years
    Create "protected void menuPersonTab_OnMenuItemClick()" function with 0 arguments
  • Yuriy Faktorovich
    Yuriy Faktorovich about 13 years
    That method is a void and I don't think gdubs meant to output anything inside the href from the method.