Click here to Skip to main content
15,898,996 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi ,

Can you please advice on teh correct code to trigger a button click event from code behind.
So far i have this much.

C#
function refresh() {
        alert('refreshing..');
        var clickButton = document.getElementById("<%= btnDelete.ClientID %>");
        clickButton.click();
    }


XML
ClientScriptManager script = Page.ClientScript;
               script.RegisterClientScriptBlock(this.GetType(), "Alert", "<script type=text/javascript>refresh();</script>");


thanks.
Posted

 
Share this answer
 
hi,

try this one.

HTML
document.getElementById('<%= btnDelete.ClientID %>').click();


Thanks,
Viprat
 
Share this answer
 
v3
Hi,

Button event is always trigger when you click the button but if you want to raise your button click event manually from code then you can use Button.PerformClick() from your code.

Suppose on click of your Button1 you want to trigger Button2 click event then Please refer this[^] MSDN example.

Let me know if you have any doubt.

Thanks
-Amit Gajjar
 
Share this answer
 
Here is the full code

<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" xmlns:asp="#unknown" />
      <input type="hidden" name ="__EVENTTARGET" value =""/>
      <script type="text/javascript">
          function __doPostBack(eventTarget, eventArgument) {
              var frm = document.forms[0];
              document.getElementById("__EVENTTARGET").value = eventTarget;
              frm.submit();
          }

          __doPostBack('Button1');//remember to add this line to some event. Otherwise the code will run infinite times.

      </script>


In code behind
protected void Page_Load(object sender, EventArgs e)
{
    if (Request["__EVENTTARGET"] == "Button1")
    {
        Button1_Click(this, null);
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
     //do your stuff
}


Hope this helps
cheers
 
Share this answer
 
try! this jQuery will do,
JavaScript
$("#button_ID").trigger('click');
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900