0

So, I have a button and I want to click on it via JavaScript. My code is

<button ID="btnTransfer" OnClick="return submeterBotao()">Submeter</button>

        <asp:Button runat="server" id="submeterButton" OnClick="submeter"/>

        <script>
            function submeterBotao() {
                var button = document.getElementById("submeterButton");
                button.click();
                return false;
            }
        </script>

The submeter() function in C# only Redirect the page. But when I click on Submeter it just refreshes the page.

Can anyone help me? Thanks!

-------UPDATE---------

I just did this

<asp:Button runat="server" id="submeterButton" OnClick="submeter" OnClientClick="submeterBotao()" Text="Submeter Caso"/>

        <script>
            function submeterBotao() {
                alert("I am an alert box!");
            }
        </script>

and it seems to work just fine!

StackJP
  • 541
  • 1
  • 5
  • 14
  • https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – MatthewMartin Mar 04 '15 at 16:39
  • And this: https://stackoverflow.com/questions/14257033/redirecting-to-new-page-asp-net – MatthewMartin Mar 04 '15 at 16:39
  • You may want to look at this thread: http://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript – Justin Mar 04 '15 at 16:40
  • Everyone's comments here aren't correct. To do what you need to do, just use this one line: `__doPostBack('submeterButton','OnClick');`. This will "emulate" clicking an ASP.NET button. – Icemanind Mar 04 '15 at 16:44
  • And add that line in the javascript funtion? or in other part of the code? – StackJP Mar 04 '15 at 16:48
  • @JoanaPagaime - Describe exactly what you are trying to do – Icemanind Mar 04 '15 at 17:01

0 Answers0