locked
How do I simulate clicking an ASP.NET button in C# code behind? RRS feed

  • Question

  • User-977364825 posted

    I am looking for ASP.NET C# code that will allow me to simulate the clicking of a Button Control, not merely a way of executing the code behind when a button is clicked.

    I have tried the following but unsuccessfully:

    btnButton_Click(btnButton, new EventArgs());

    Next I tried:

    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Click", "temporaryButtonClick()", true);

    together with:

    function temporaryButtonClick() { document.getElementById("<%= btnTemporaryButton.ClientID %>").click();}

    But again no joy!

    A working solution would be most appreciated.

    Thank you

    Tuesday, September 18, 2018 11:46 AM

All replies

  • User-2015242085 posted

    Hi

    If need to trigger the click event in the client side with jquery ...

    $("#<%= btnTemporaryButton.ClientID %>").click();

    If you need to trigger that from the server side yes you can like ...

    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Do-click", "$('#<%= btnTemporaryButton.ClientID %>').click();", true);

    Also you can have a void function and called from the server side, inside the btn click or in another place

    Hope that help

    Wednesday, September 19, 2018 1:37 AM
  • User-1952463932 posted

    In your RegisterStartupScript replace "temporaryButtonClick()", with $"document.getElementById(\"{btnButton.ClientID}\").click()".  This will execute the client side button click first so whatever listeners there are will get the event including the asp button which will do the postback.

    Wednesday, September 19, 2018 3:47 AM
  • User-893317190 posted

    Hi insfazee ,

    I have tried your code , it successfully  causes the  server side click event.

    Below is my code.

     <form id="form1" runat="server">
     
    
            <asp:Button ID="btnTemporaryButton" runat="server" Text="Button" OnClick="Button1_Click" />
            <script>
    
                 function temporaryButtonClick() { document.getElementById("<%= btnTemporaryButton.ClientID %>").click();}
            
        </script>
            
        </form>

    Code behind.

     protected void Page_Load(object sender, EventArgs e)
            {
                ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Click", "temporaryButtonClick()", true);
            }

    I have also tried a link to simulate the click event , also have a success.

       <form id="form1" runat="server">
           <a href="#" onclick="document.getElementById('<%=btnTemporaryButton.ClientID %>').click()">click me</a>
    
            <asp:Button ID="btnTemporaryButton" runat="server" Text="Button" OnClick="Button1_Click" />
                   
        </form>

    Don't know what you actually meet using your code.  If you want to trigger client side event , just do as Primillo has shown.

    You could also use F12 developer tool to see if there is any error.

    If you still have a problem, please post more of your code to let us reproduce your problem.

    Best regards,

    Ackerly Xu

    Wednesday, September 19, 2018 6:12 AM
  • User-977364825 posted

    The original code I tried should have worked but there was another control on the page preventing this from occurring!

    Thank you to everyone who responded.

    Friday, September 21, 2018 7:17 PM
  • User-1029046233 posted

    Html:

    <asp:Button ID="btnTemporaryButton" ClientIDMode="Static" runat="server"

    Code Behind:

    1#

    ClientScript.RegisterStartupScript(GetType(), "Do-click", "$('#btnTemporaryButton').click();", true);

    or

    2#

    string Clk = "clkbnt();";

    ClientScript.RegisterStartupScript(GetType(), "Bntclick", Clk, true);

    Client side for #2

    <script>

    function clkbnt(){

    document.getElementById('btnTemporaryButton').click()

    }

    </script>

    Thursday, January 31, 2019 8:56 PM