41

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?

0

10 Answers 10

47

You can just place this line in a JavaScript function:

__doPostBack('btnSubmit','OnClick');

Or do something like this:

$('#btnSubmit').trigger('click');
Sign up to request clarification or add additional context in comments.

4 Comments

does this line call the server side event?
@pratapk can you update that comment link (maybe add to answer) I am interested and it's dead.
You forgot to mention that the second example needs jQuery and that ASP Net Forms never generates such nice ids.
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>

2 Comments

Really appreciated, no one mentioned any of above will not work in Visible='False'. The <div hidden></div> worked great for me. Cheers..:)
style="display:none;" directly on the button works too.
16

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

var clickButton = document.getElementById("<%= btnClearSession.ClientID %>");
clickButton.click();

1 Comment

it's not working for me. the page refreshes but the code never run the code behind
8

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"/>

2 Comments

Visible="false" this is why it is not working, just hide it with <div hidden></div>
Looking back on this - what we really needed to do was use an Ajax call to a server side function rather than relying on WebForms.
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...

Comments

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?

Comments

1

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.

Comments

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.

Comments

0
document.FormName.btnSubmit.click(); 

works for me. Enjoy.

Comments

0
$("#"+document.getElementById("<%= ButtonID.ClientID %>")).trigger("click");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.