What I’m trying to do is to call function by a button.
i'll leave you with a simple code that doesn't work!:
@{
protected void print()
{
@<p>WELCOME!</p>
}
}
<form>
<button onclick="print" value="CLICK ME"/>
</form>
any idea on how to accomplish what im trying to do in the code above?
NOTE: IM NOT USING MVC
When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
Just declare the C function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code.
We can call a C function from Python program using the ctypes module.
I know this is an old question, but this is the first result on google when you look up how to run an OnClick
method in ASP Razor, and I think there is a better way to do this than the currently accepted answer. I don't know if this is new as of the writing of the original answer, but I do think it is the best way to handle this behavior because it doesn't require hand-writing of AJAX or JavaScript methods.
For those coming from Web Forms into ASP Razor, perhaps the best (and easiest) way to recreate that type of behavior is to use a Handler Method. Handler Methods are appended to the Get and Post methods, and can be run using forms generated by ASP Razor.
By default, your cshtml.cs page will have a function which looks like this:
public async Task OnPostAsync()
{
<Do Post Stuff Here>
}
Sometimes though, you want to do something specific depending on what exactly caused the post. This is where you can implement Handler Methods.
public async Task OnPostButton()
{
<Do button stuff here>
}
If you then want to use the button method, you simply create an ASP Button that indicates its handler method.
<form asp-page-handler="button" method="post">
<button class="btn btn-default">Button</button>
</form>
This will tell razor pages to add a reference to the Button Handler Method in the querystring of the resulting button, like so.
<form method="post" action="/page?handler=button">
A visit to that will tell Razor to use the named handler method. As long as the name of the handler matches the name of the function and HTTP Method, it will run the function.
Your code would look like this:
@{
protected void print()
{
@<p>WELCOME!</p>
}
public async Task OnPostPrint()
{
print();
}
}
<form asp-page-handler="Print" method="post">
<button class="btn btn-default">CLICK ME</button>
</form>
Don't forget, this will only call the OnPostPrint method. If you need to run stuff every time you post, it needs to be in that method too. Probably best to break those tasks out into a separate function, and then call that from within the post methods. Makes it easier to maintain.
For more info on Method Handlers, including how to add variables to them, check out Mikes DotNetting! He did a great job of explaining this thoroughly, and I feel I learned a lot from his article.
https://www.mikesdotnetting.com/article/308/razor-pages-understanding-handler-methods
You can't do it like this.It is not ASP.NET WebForms
.
So if you want to Execute a C# function on Button click in Razor, you must could create a Controller
,then when user clicks a button you must can call a javascript function and it sends an ajax request to your controller then get the data (if there is data) and display it.
Update: Here is an alternative simple sample about how to do this:
In your Controller Add this Method:
public ActionResult GetMessage()
{
string message = "Welcome";
return new JsonResult {Data = message,JsonRequestBehavior = JsonRequestBehavior.AllowGet};
}
And in your View (HTML):
<input type="button" onclick="GetMessage()" value="Get Message"/>
<p></p>
JavaScript:
function GetMessage() {
$.get("/Home/GetMessage", function (data) {
$("p").html(data);
});
}
And don't forget to import jQuery library:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
PS: I assume your Controller name is HomeController, you must change the url if it has different name:
$.get("/{Controller-Name}/{Action-Name}", ...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With