User Id :    Password :      New Member   Forgot Password  
How to call button click event from java script function
Description In many cases we need to call button click event from java script. So in this article I am going to describe how can we achieve this..   No. of Views     2344
  Rating     0
Author Sumit Gupta   Posted On     23 Apr 2011
Tags ASP.NET,Java Script    

Sample Code   Download Code

Let's say we want to call button click event on body load event.

So the first step to design a html form with two hidden field to store the object sender and event arguments value as follows:

<bodyy onload="javascript:__doPostBack('Button1','')">
    <formm id="Form1" runat="server">
    <input type ="hidden" name ="__EVENTTARGET" value =""/>
<input type ="hidden" name ="__EVENTARGUMENT" value =""/>
<asp:Button ID="Button1" runat="server" Text="Button" 
            onclick="Button1_Click" />

Then write the java script function in the head section:

 <script type="text/javascript">
        function __doPostBack(eventTarget, eventArgument) {
            document.Form1.__EVENTTARGET.value = eventTarget;
            document.Form1.__EVENTARGUMENT.value = eventArgument;

Last step to put the following code in .cs file:

    protected void Page_Load(object sender, EventArgs e)
        if (Request.Form["__EVENTTARGET"] == "Button1")
            //fire event
            Button1_Click(this, new EventArgs());

 protected void Button1_Click(object sender, EventArgs e)
        //write your code here...        


About Author

About Author I am Sumit Gupta working in 3 Pillar Global Pvt. Ltd as Module Lead. I have 7+ year of experience in .Net technologies. I love to explore new technologies and write technical article. Sumit Gupta
No Photo
Country India
Company 3 Pillar Global Pvt. Ltd.
Home Page

Rate this article

Rating options from poor, fair, good, very good to excelent.  


Write your comment here.
Verification Code