-2

I have a simple button

 <input  type="submit" value="Global" onclick="Global_Click"  />

that when clicked should search into a db and here is the event that it should call:

protected void Global_Click(object sender, EventArgs e)
        {
            FillUsers("global");
        }

FillUsers is the actual method that searches id DB. When I click the button a JavaScript error is displayed: Global_Click is not defined! Why is that if I am not using JavaScript? And how can I call the Global_Click from the button?

Antwane
  • 20,760
  • 7
  • 51
  • 84
George Great
  • 243
  • 1
  • 3
  • 14
  • Possible duplicate of [ASP.NET MVC - How to call void controller method without leaving the view?](http://stackoverflow.com/questions/26833065/asp-net-mvc-how-to-call-void-controller-method-without-leaving-the-view) – Dr. Roggia Feb 15 '17 at 09:25
  • This has nothing to do with MVC (its web forms code - there are no events in MVC). And `onclick` calls a javascript method, not code in your controller. –  Feb 15 '17 at 09:25
  • @StephenMuecke actually it has to do with MVC, I did not copy all the code, is not usefull. **@using (Html.BeginForm())** sounds familiar? – George Great Feb 15 '17 at 09:27
  • There is no such thing as `protected void Global_Click(object sender, EventArgs e)` in asp.net-mvc. I suggest you go to the MVC site and work through the tutorials to learn the basics. –  Feb 15 '17 at 09:28
  • @StephenMuecke that could be easily solved with public . I used in other classes (that works) public . I don't see a problem with this, neither with the other one. If you know an answer to this, you are my guest. – George Great Feb 15 '17 at 09:33
  • No it cannot. What are you trying to do here? You have a submit button inside a form which by default will post back to an action method marked with `[HttpPost]` in your controller. I assume you want to call another method and return some results so you might have another method in your controller (say) `public ActionResult DoSomething(....) { .... return Json(...); }` and then you handle the click event using javascript and call the method using ajax. –  Feb 15 '17 at 09:38

1 Answers1

0

You miss the runat="server" attribute.

In your code,in that way,it is called javascript handler.

Moonfred
  • 14
  • 3