We help IT Professionals succeed at work.

How to fire ASP.Net button click even from JavaScript

P1ST0LPETE
P1ST0LPETE asked
on
Ok, so I have a hidden asp.net button on my page that looks like this:

<asp:Button ID="btHiddenButton" runat="server" OnClick="HiddenButton_Click" />

Then I am trying to programmatically launch the button click event with JavaScript like this:

function BlastChillTypeChange(dropdownlist)
{
      alert('hit')

        //Fire the hidden button to initiate server side code:
        //document.getElementById('btHiddenButton').click();
        __doPostBack('btHiddenButton', 'OnClick');
      
}

I know the javascript function is being triggered due to the alert(), but my breakpoints in my asp.net code behind are never hit, indicating that I'm not calling the postback correctly.

What am I doing wrong?
Comment
Watch Question

Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Hi P1ST0LPETE, 
Try document.getElementById("btHiddenButton").click();
Regards,
Chinmay


Commented:
The ID of the button probably isn't btHiddenButton anymore.
Either check the source for the new ID or use something like: <% btHiddenButton.ClientID %>

Hope this helps...
Commented:
$.02 =

1) your alert has syntax issue; needs semi-colon... likely not part of your issue, but I'd hate to see it cause you problems as you work on this.

2) element.click() will not work with IE... you likely knew that as you had it commented out

3) you are implementing __doPostBack() function correctly but a few things might be getting in your way
A) As Onthrax pointed out if the ID has been modified by the INamingContainer convention you will have problems....checking view source should allow you to verify this.
B) When debugging you may get an "Object Expected" error from JavaScript when trying to run the code. This happens when the hidden fields for __EVENTTARGET and __EVENTARGUEMENT are not on the page. If you haven't put a control on the page that implements these two fields your __doPostBack() will always fail. Drag a <asp:DropDownList id="ddl" runat="server" AutoPostBack="true" /> and these fields will be generated and your __doPostBack will then work.

Almost any <asp> control will work excepting buttons and imagebuttons, but it has to be set to cause a postback or it will not generate the fields (__EVENTTARGET and __EVENTARGUEMENT).

Commented:
HI! You may try

document.getElementById("button1").onclick();
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019
Commented:
Hi P1ST0LPETE,

Sorry my bad, generally I use DHTML a lot and I was under the same impression so I suggested that solution

Here is there right one :  document.getElementById("<%= Button1.ClientID %>").click();
Also @Onthrax object.click() works absolutely fine under IE
Regards,
Chinmay


Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Again clicked submit too fast
document.getElementById("<%= btHiddenButton.ClientID %>").click();

Commented:
@Chinmay_Patel: it wasn't me that said .click didn't work under IE ;)
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Oh My God!!! I am so sorry :( Really Really Sorry :(
it was for ddayx10.


Commented:
I'll forgive you... but just this once! Don't let it happen again!! ;) :P
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
:D Sure Mate. I promise :)

Author

Commented:
Ok, turns out it was a syntax error on my part, but only because I had changed the name of the hidden button ID, and failed to update it's name in javascript :-)

I did end up using element.click() method, which works fine in IE8.  I'm not sure if element.click() is supported in earlier versions of IE, but for this app I don't care because for this specific app I have the ability to require the users to update their browsers to the latest version.