Grid中的输入按钮事件不使用JQuery触发

时间:2013-01-14 17:49:31

标签: jquery asp.net button jquery-click-event

我的gridview中有一个输入按钮。我正在使用jquery捕获按钮的单击事件,如下所示(后面正在尝试实现电子邮件的读取未读功能):

  $('.toggleBtn').click(function(e) {
           btnUnreadClicked = true;
$.ajax({
                type: "POST",
                url: "ClaimDetails.aspx/BtnOpenPDF",
                data: "{'id': '" + letterid + "','anchText': '" + anchText + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,

                success: function (msg) {
                    if (msg.d == 'Read') {
                        $tr.removeClass('unreadGridRow');
                        //update unread text to date time now
                        var d = new Date();
                        var replaceUnread = unreadColumn.toString().replace("Unread", d.format("dd/MM/yyyy"));
                        $tr.html(replaceUnread);

                        //update claim letter counts only
                        var parentRow = document.getElementById('__' + parentrowid).innerHTML;
                        var lblUnreadDocCount = $('#__' + parentrowid).find('#lblUnreadDocCount').text();
                        var finalCount = parseInt(lblUnreadDocCount) -1 ; 
                        if(finalCount >= 0)
                        {
                            $('#__' + parentrowid).find('#lblUnreadDocCount').text(finalCount);
                        }

                        if(finalCount == 0 )
                        {
                            //remove the class
                            $('#__' + parentrowid).removeClass('unreadGridRow');
                        }

                        //get count of overall documents and subtract from it
                        var notification = $("#NewLetter3");
                        var numb = notification.text().match(/\d/g);
                        var finaldigits = numb.toString().replace(",", "");
                        var finalTotalDocsCount = parseInt(finaldigits) - 1;
                        if(finalTotalDocsCount >= 0)
                        {      
                            //notification.text(notification.text().replace(numb, finalTotalDocsCount));
                            notification.html("You have " + finalTotalDocsCount + " unread Documents. Please click here to view");
                            //document.getElementById("NewLetter2").innerHTML = notification.text(); 
                            if(finalTotalDocsCount == 0)
                            {
                                $("#NewLetter2").hide();  
                            }
                        }

                        $tr.find(".toggleBtn").text('Un-read');
                    }
                    else  if (msg.d == 'Un-read') {
                         $tr.addClass('unreadGridRow');
                        //update unread text to date time now
                        var unreadDate = new Date();
                        var replaceRead = unreadColumn.toString().replace("Unread", unreadDate.format("dd/MM/yyyy"));
                        $tr.html(replaceRead);

                        //update claim letter counts only
                        var unreadparentRow = document.getElementById('__' + parentrowid).innerHTML;
                        var lblUnreadDocCountUnread = $('#__' + parentrowid).find('#lblUnreadDocCount').text();
                        var finalCountUnread = parseInt(lblUnreadDocCountUnread) + 1 ; 
                        if(finalCountUnread >= 0)
                        {
                            $('#__' + parentrowid).find('#lblUnreadDocCount').text(finalCountUnread);
                        }

                        if(finalCountUnread > 0 )
                        {
                            //remove the class
                            $('#__' + parentrowid).addClass('unreadGridRow');
                        }

                        //get count of overall documents and subtract from it

                        var Unreadnotification = $("#NewLetter3");
                        if(Unreadnotification.text() == "" ) {    Unreadnotification.html("You have 0 ");}
                        var Unreadnumb = Unreadnotification.text().match(/\d/g);
                        var Unreadfinaldigits = Unreadnumb.toString().replace(",", "");
                        var finalTotalDocsCountUnread = parseInt(Unreadfinaldigits) + 1;

                        if(finalTotalDocsCountUnread >= 0)
                        {                                 
                            if(finalTotalDocsCountUnread >= 0)
                            {
                                $("#NewLetter2").show();  
                            }
                            //notification.text(notification.text().replace(numb, finalTotalDocsCount));
                            Unreadnotification.html("You have " + finalTotalDocsCountUnread + " unread Documents. Please click here to view");
                            //document.getElementById("NewLetter2").innerHTML = notification.text(); 


                        }

                        $tr.find(".toggleBtn").text('Read');
                    }
              }
            });
在上述事件之后的同一页面上的

我有另一个捕获行点击的事件,如下所示:

     $("#<%=gvClaimDtSentDate.ClientID%> tr:has(td)").click(function (e) {
                if( $(this).closest('tr').hasClass('parent'))
                {
 if(btnUnreadClicked == false)
                {
                 // do stuff related to row click
                }

                btnUnreadClicked = false;

现在,当我第一次点击按钮时它会正常运行,但是在第二次点击时,事件没有被调用,并且tr点击内部的功能被运行...

但是当我刷新页面并做同样的事情然后再次刷新时,按钮事件就会运行...

此外,当我注释掉ajax部分时,它很好......我似乎也找不到任何语法错误。

2 个答案:

答案 0 :(得分:1)

更改以下代码

 $('.toggleBtn').click(function(e) {

以下

$("body").on("click", ".toggleBtn", function(e){

我认为这就是你所需要的。

这里我将事件添加到body,因为我不知道您的HTML结构。您应该将.on附加到datagridview或其父级,以便事件不必冒泡太多。

<强>更新

在每一行上追加事件并不是一个好主意,尤其是当行数太多时。它会使脚本太沉重。因此,建议在这些情况下使用.on。在旧版本的jQuery中,您可能必须使用.live而不是.on

答案 1 :(得分:0)

尝试以下

function pageLoad(sender, args) {
    $('.toggleBtn').click(function(e) {...}
}

这应该有效。实际上在asp.net中,您应该在 pageLoad()函数中附加所有事件。