如何在单击html表中的按钮时调用javascript函数?

时间:2015-10-23 07:29:20

标签: javascript jquery html asp.net webforms

更新先生anmarti 你好,先生,谢谢你回答我的问题 我将此代码添加到

的Page_Load

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

在页面加载(运行时)上,label1将其文本更改为"没有方法"

然后我点击按钮然后弹出窗口进来。我选择了YES按钮但没有发生任何事情它甚至没有关闭弹出窗口

为了检查javascript是否正常工作我更改了此代码
__doPostBack(" DeleteRecord",''); 至 $(" [ID * = btnadd]&#34)。单击();

点击弹出的是按钮后,点击按钮BTNADD

我的问题先生,为什么__doPostBack(" DeleteRecord",'');不对我有用吗?我甚至尝试了一些在线发布的教程,但它仍然是相同的

这是javascript函数:

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

以下是

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

Html代码

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

它完美无缺

但是如何调用html表中的函数

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

1 个答案:

答案 0 :(得分:1)

客户端

创建一个打开对话框并在editurl: 'ListAjaxAccess.cfm?functionName=deleteMember&functionParams={"id":"341"}' 事件中调用它的函数:

onClick

服务器端代码

如果您进行手工制作 <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> ,则必须在__doPostback()EventTarget上检索EventArgumentsPage_Load()(如果有)您更喜欢当前页面的事件处理程序:

Page_Init()