40

How do I fire a server side button click event from JavaScript?

I tried like this:

document.getElementById("<%= ButtonID.ClientID %>").click();

But no use. How can I do it?

Ruben
  • 699
  • 2
  • 16
  • 36
Venky
  • 401
  • 1
  • 4
  • 4

10 Answers10

46

You can just place this line in a JavaScript function:

__doPostBack('btnSubmit','OnClick');

Or do something like this:

$('#btnSubmit').trigger('click');
Rob
  • 26,989
  • 16
  • 82
  • 98
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
23
var clickButton = document.getElementById("<%= btnClearSession.ClientID %>");
clickButton.click();

That solution works for me, but remember it wont work if your asp button has

Visible="False"

To hide button that should be triggered with that script you should hide it with <div hidden></div>

Sebastian 506563
  • 6,980
  • 3
  • 31
  • 56
16

I used the below JavaScript code and it works...

var clickButton = document.getElementById("<%= btnClearSession.ClientID %>");
clickButton.click();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ruchir Agarwal
  • 169
  • 1
  • 2
7

None of the solutions posted here would work for me, this was my eventual solution to the problem.

// In Server Side code
protected void Page_Load(object sender, EventArgs e)
{
    Page.GetPostBackEventReference(hiddenButton);
}

// Javascript
function SetSaved() {
    __doPostBack("<%= hiddenButton.UniqueID %>", "OnClick");
}

// ASP
 <asp:Button ID="hiddenButton" runat="server" OnClick="btnSaveGroup_Click" Visible="false"/>
Amicable
  • 3,115
  • 3
  • 49
  • 77
3

I lived this problem in two days and suddenly I realized it that I am using this click method(for asp button) in a submit button(in html submit button) javascript method...

I mean ->

I have an html submit button and an asp button like these:

<input type="submit" value="Siparişi Gönder" onclick="SendEmail()" />
<asp:Button ID="sendEmailButton" runat="server" Text="Gönder" OnClick="SendToEmail" Visible="True"></asp:Button>

SendToEmail() is a server side method in Default.aspx SendEmail() is a javascript method like this:

<script type="text/javascript" lang="javascript">
   function SendEmail() {
            document.getElementById('<%= sendEmailButton.UniqueID %>').click();
            alert("Your message is sending...");
        }
</script>

And this "document.getElementById('<%= sendEmailButton.UniqueID %>').click();" method did not work in just Crome. It was working in IE and Firefox.

Then I tried and tried a lot of ways for executing "SendToEmail()" method in Crome.

Then suddenly I changed html submit button --> just html button like this and now it is working:

<input type="button" value="Siparişi Gönder" onclick="SendEmail()" />

Have a nice days...

Can OTUR
  • 51
  • 3
1

I can make things work this way:

inside javascript junction that is executed by the html button:

document.getElementById("<%= Button2.ClientID %>").click();

ASP button inside div:

<div id="submitBtn" style="display: none;">
   <asp:Button ID="Button2" runat="server" Text="Submit" ValidationGroup="AllValidators" OnClick="Button2_Click" />
</div>

Everything runs from the .cs file except that the code below doesn't execute. There is no message box and redirect to the same page (refresh all boxes):

 int count = cmd.ExecuteNonQuery();
 if (count > 0)
 {
   cmd2.CommandText = insertSuperRoster;
   cmd2.Connection = con;
   cmd2.ExecuteNonQuery();
   string url = "VaccineRefusal.aspx";
   ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Data Inserted Successfully!');window.location.href = '" + url + "';", true);
  }

Any ideas why these lines won't execute?

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Zaichusha
  • 21
  • 3
0
document.FormName.btnSubmit.click(); 

works for me. Enjoy.

Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
0
$("#"+document.getElementById("<%= ButtonID.ClientID %>")).trigger("click");
shruti1810
  • 3,920
  • 2
  • 16
  • 28
Farhad Manafi
  • 334
  • 1
  • 8
  • 17
0

The issue I had was the validation group that was not specified. I added ValidationGroup="none" as per below and it worked.

<asp:Button ID="BtnQuickSearch" runat="server" Text="Search" 
OnClick="BtnQuickSearch_Click" ValidationGroup="none" />

I must mention that I had 2 other forms with buttons on the page, both had their own validation groups specified. This button had did not have a validation group specified and the onclick event simply did not fire.

0

You can fill a hidden field from your JavaScript code and do an explicit postback from JavaScript. Then from the server side, check that hiddenfield and do whatever necessary.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Boomer
  • 1,468
  • 1
  • 15
  • 19