Ron Miller
asked on
C# click event
Hi I am using ASP.Net MVC with C# and I have a webpage with some buttons. Is there a way I can simulate clicking one of the buttons from code behind in the controller? Thank you.
ASKER
Hi thank you for your answer, on this occasion I am using a button element not an input type button.
CAn you post the button html?
document.getElementById("~ ~button id~~").click();
This will fire the click event of whichever element you put the id in for.
If using JQuery $("#~~button id~~").click();
This will fire the click event of whichever element you put the id in for.
If using JQuery $("#~~button id~~").click();
ASKER
Hi here is my code:
<div class="col-md-4 text-center">
<button id="client-hangup-call-but ton" class="btn btn-lg btn-danger">
Hang up
</button>
</div>
On this I've got a callback event that fires when a call is ended. This event is handled from my MVC controller and I would need the button to be disabled when that happens. Here is my action from my controller:
[HttpPost]
public async Task<ActionResult> ClientHangUpCall()
{
...
return new EmptyResult();
}
<div class="col-md-4 text-center">
<button id="client-hangup-call-but
Hang up
</button>
</div>
On this I've got a callback event that fires when a call is ended. This event is handled from my MVC controller and I would need the button to be disabled when that happens. Here is my action from my controller:
[HttpPost]
public async Task<ActionResult> ClientHangUpCall()
{
...
return new EmptyResult();
}
From code behind you will need to use the ScriptManager object. If your button is in an update panel you will want to use this:
and if you are in a callback event (page reloads) use
What this will do is put a script element on your page with the call to the click event. You may need to modify the script block to enable the button first, then call the click, then disable it again. Another option is to simply call the handler function. A third is to have a hidden button that handles the event in MVC and call the click event for that button from client-hangup-call-button as well as the script. I am not familiar with MVC so not sure of the call stack for this.
string script="Document.getElementById("client-hangup-call-button").click();";
ScriptManager.RegisterClientScriptBlock(
this,
typeof(Page),
"Click_client-hangup-call-button",
script,
true);
and if you are in a callback event (page reloads) use
string script="Document.getElementById("client-hangup-call-button").click();";
ScriptManager.RegisterStartupScript(
this,
typeof(Page),
"Click_client-hangup-call-button",
script,
true);
What this will do is put a script element on your page with the call to the click event. You may need to modify the script block to enable the button first, then call the click, then disable it again. Another option is to simply call the handler function. A third is to have a hidden button that handles the event in MVC and call the click event for that button from client-hangup-call-button as well as the script. I am not familiar with MVC so not sure of the call stack for this.
ASKER
Hi Geoff,
Thanks for your reply, how do I get the Page object as I'm in the controller class?
Thanks for your reply, how do I get the Page object as I'm in the controller class?
The Page object is stored in your HttpContext: Page page = HttpContext.Current.Handle r as Page;
If you are calling a function outside of your page class you can either pass the page or the context. Another option is to return the script you want and add it to the scriptmanager code.
See https://stackoverflow.com/questions/58123/get-current-system-web-ui-page-from-httpcontext. There is some great info there.
If you are calling a function outside of your page class you can either pass the page or the context. Another option is to return the script you want and add it to the scriptmanager code.
See https://stackoverflow.com/questions/58123/get-current-system-web-ui-page-from-httpcontext. There is some great info there.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
1) In the same controller call the method directly or
2) In a different controller use RedirectToAction method