简体   繁体   中英

How to call a javascript function when a button inside html table is clicked?

update sir anmarti hello sir thank you for answering my questions i add this code to

page_load

string eventTarget = this.Request["__EVENTTARGET"];
        if (eventTarget == "DeleteRecord")
        {
            Label1.Text = "Method called!!!";
            // Delete your record here
        }else
        {
            Label1.Text = "No method";
        }

on page load(run time) the label1 change its text to "No method"

then i clicked the button then the pop up comes in. i choosed the YES button but nothing happend it didn't even close the pop up

in order to check if the javascript works correctly i changed this code
__doPostBack("DeleteRecord", ''); to $("[id*=btnadd]").click();

and after clicking the yes button of pop up it clicked the button BTNADD

my question sir is why do __doPostBack("DeleteRecord", ''); doesn't work on me? i even tried some posted tutorials online but its still the same


This is the javascript function:

    <script type="text/javascript">
   $(function () {
       //$("[id*=btnDelete]").removeAttr("onclick");
       $("[id*=btnDelete]").removeAttr("onclick");
       $("#dialog").dialog({
           modal: true,
           autoOpen: false,
           title: "Confirmation",
           width: 350,
           height: 160,
           buttons: [
           {
               id: "Yes",
               text: "Yes",
               click: function () {
                   $("[id*=btnDelete]").attr("rel", "delete");
                   $("[id*=btnDelete]").click();
               }
           },
           {
               id: "No",
               text: "No",
               click: function () {
                   $(this).dialog('close');
               }
           }
           ]
       });
       $("[id*=btnDelete]").click(function () {
           if ($(this).attr("rel") != "delete") {
               $('#dialog').dialog('open');
               return false;
           } else {
               __doPostBack(this.name, '');
           }
       });
   });
</script>

Here is the code behind

    protected void DeleteRecord(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Record Deleted.')", true);
    }

Html code

<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="DeleteRecord" UseSubmitBehavior="false" />
    <div id="dialog" style="display: none">
       Do you want to delete this record?
    </div>

it works perfectly

but how to i call the function inside html table

<tr>
   <td>
      <%# DataBinder.Eval(Container.DataItem, "signatoryid") %> 
   </td>
   <td>
      <%# DataBinder.Eval(Container.DataItem, "signatoryname") %> 
   </td>
   <td>
      <%# DataBinder.Eval(Container.DataItem, "signatoryPosition") %> 
   </td>
   <td>
      <%# DataBinder.Eval(Container.DataItem, "signatoryOffice") %> 
   </td>
   <td>
      <button type="button" class="btndelete btn btn-xs btn-danger" OnClick="DeleteRecord">
      <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>Delete
      </button>
   </td>
</tr>          

Client side

Create a function that opens the dialog and call it in the onClick event:

 <button type="button" class="btndelete btn btn-xs btn-danger" OnClick="javascript:deleteRecord();">
     <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>Delete
  </button>


<script>
  $(function () {
     $("#dialog").dialog({
        modal: true,
        autoOpen: false,
        title: "Confirmation",
        width: 350,
        height: 160,
        buttons: [
        {
            id: "Yes",
            text: "Yes",
            click: function () {
                 __doPostBack("DeleteRecord", '');
            }
        },
        {
            id: "No",
            text: "No",
            click: function () {
                $(this).dialog('close');
                return false;
            }
        }
        ]
     });
});

    function deleteRecord(){
        $("#dialog").dialog('open');
    }
 </script>

Server side code

If you do a hand-made __doPostback() you have to retrieve the EventTarget and EventArguments (if you have) on the Page_Load() or Page_Init() if you prefer, event handlers of the current page:

 protected void Page_Load(object sender, EventArgs e)
    {
      string eventTarget = this.Request["__EVENTTARGET"];
      if(eventTarget == "DeleteRecord")
      {

       // Delete your record here
      }

  }

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