How to manually submit a button to asp.net using javascript?
If you have a control like this:
<asp:Button ID="Foo" ... />
You can do something simple, like fire a 'click' event in JS when accessing the updated client id (jQuery syntax here):
$('#<%=Foo.ClientID%>').click()
Or you can make JS work like this:
<script type="text/javascript">
function clickFoo() {
<%=Page.ClientScript. GetPostBackEventReference(Foo)%>;
}
</script>
source to share
var button = document.getElementById('btnID');
if (button)
{
button.click();
}
If you can put javascript right in your .aspx markup, you can also get around the changing id by doing the following:
var button = document.getElementById('<%= myServerButton.ClientID %>');
if (button)
{
button.click();
}
When your .aspx is processed, the button id as it appears on the page will be replaced with your javascript function.
source to share
Easily you can use the __doPostBack function passing the ID of the control you want to fire the click event (command, etc.).
To avoid problems with the ID, do something like this:
__doPostBack("<%= yourConrol.UniqueID%>");
EDIT: There is an existing .Net Framework Page.GetPostBackEventReference method that emits a client-side script that triggers the postback and also provides a reference to the control that triggered the postback event.
source to share