简体   繁体   中英

asp.net Custome Control and javascript

I have master page with custom button control. I have one javascript function in master page. When i try to call this javascript function with in custom control code behind its not invoking. pls check below code.

Javascript code in master page

  function loader()
  {
  //Note: ctl00_loader is master page control.
  document.getElementById('ctl00_loader')="X"; 
  }

Mycustom control is

   <Shri:ConfirmBtn ID="UsrBtn" runat="server">

Code-behind code is

  Protected Sub UsrBtn_ConfirmClick(ByVal sender As Object, ByVal e As 
    System.EventArgs) Handles UsrBtn.ConfirmClick
  ClientScript.RegisterStartupScript(GetType(String), "MyCode1", "<Script 
   Language=Javascript>loader();</script>")
 End sub

code behind line is working fine in page loading time. but when i place it custom control click (UsrBtn_ConfirmClick) inside its not loading. how to solve this one? any idea please.

Use jQuery:

In the header of your html page:

<script src="https://code.jquery.com/jquery-3.3.1.js" type="text/javascript"></script>

Then capture the button click

$("#<%=UsrBtn.ClientID%>").bind("ConfirmClick", function () {
    // Do whatever you need here ....
});

To debug code embedded in the page add the keyword debugger; like so:

$("#<%=UsrBtn.ClientID%>").bind("ConfirmClick", function () {
    // Do whatever you need here ....
    debugger;
});

This will force the IDE to stop on that line then you can use the F10 key to step through the code like you would in the code behind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM