Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire a button click event from JavaScript in ASP.NET

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?

like image 604
Venky Avatar asked Oct 04 '11 09:10

Venky


People also ask

How do you call 2 JavaScript functions onclick of a button in asp net?

Another option is to use the onclientclick event which allows you to run a JS function w/o a server interaction. In the first button (adds a new row to the table), I have a server side onclick="btnAdd_Click" as well as a client side OnClientClick="javascript:ResetSaveFlag()" .

How do you call a button click method in C#?

private void SubGraphButton_Click(object sender, RoutedEventArgs args) { } private void ChildNode_Click(object sender, RoutedEventArgs args) { // call SubGraphButton-Click(). }

What is OnClientClick asp net?

Use the OnClientClick property to specify additional client-side script that executes when a Button control's Click event is raised. The script that you specify for this property is rendered in the Button control's OnClick attribute in addition to the control's predefined client-side script.


2 Answers

You can just place this line in a JavaScript function:

__doPostBack('btnSubmit','OnClick'); 

Or do something like this:

$('#btnSubmit').trigger('click'); 
like image 62
Glory Raj Avatar answered Sep 23 '22 13:09

Glory Raj


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>

like image 21
Sebastian 506563 Avatar answered Sep 25 '22 13:09

Sebastian 506563