Link to home
Start Free TrialLog in
Avatar of Ron Miller
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.
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

If the button are submit buttons just call the associated controller method:
1) In the same controller call the method directly or
2) In a different controller use RedirectToAction method
Avatar of Ron Miller
Ron Miller

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();
Hi here is my code:

<div class="col-md-4 text-center">
                        <button id="client-hangup-call-button" 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();
        }
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:

string script="Document.getElementById("client-hangup-call-button").click();";

ScriptManager.RegisterClientScriptBlock(
            this,
            typeof(Page),
            "Click_client-hangup-call-button",
            script,
            true);

Open in new window


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);

Open in new window


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.
Hi Geoff,

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.Handler 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.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members 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.