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?

Answers:

Answer

You can just place this line in a JavaScript function:

__doPostBack('btnSubmit','OnClick');

Or do something like this:

$('#btnSubmit').trigger('click');
Answer
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>

Answer

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

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

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...

Answer

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?

Answer

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.

Answer
document.FormName.btnSubmit.click(); 

works for me. Enjoy.

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

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.