2015-08-20 80 views
0

如果用戶使用類註釋點擊顯示註釋按鈕,彈出窗口打開。想要填充此彈出窗口(gridview是彈出窗口的內容),併爲該項目添加註釋。 HTML:使用ajax調用在網格視圖中顯示評論

<div class="pop-up-comments"> 
     <div class="pop-up-wrap-comments"> 
      <div class="edit-form"> 
       <asp:GridView ID="GridView1" runat="server"> 
        <Columns> 
         <asp:BoundField DataField="CommentText" HeaderText="CommentText" /> 
         <asp:BoundField DataField="Date" HeaderText="Date" /> 
        </Columns> 
       </asp:GridView> 
      </div> 
      <div class="user-controls"> 
       <button class="close-popup btn btn-danger">Close</button> 
      </div> 
     </div> 
    </div> 

JS

$(document).on("click", ".comments", function() { 
    var clicked = $(this); 
    var id = clicked.attr("data-id"); 
    alert(id); 
    $.ajax({ 
     type: "POST", 
     url: "Admin.aspx/ShowComments", 
     data: id, 
     dataType: "json", 
     contentType: "application/json; charset=utf-8", 
     success: function (data) { 
      var returnedstring = data.d; 
      alert(returnedstring); 
     } 
    }); 
    $('.pop-up-comments').addClass('show-popup'); 
}); 

C#

[WebMethod] 
public static string ShowComments(string id) 
      { 
       return id; 
      } 

斷點在此的WebMethod永遠不會觸發。爲什麼?以及如何將註釋數組發送到datagridview?

+0

如果你打開瀏覽器的開發者工具,是一個有頁面方法POST上的HTTP 500? –

+0

@KarlAnderson是的。 http://i.imgur.com/GNKm1OZ.png – LoverBugs

回答

1

ASP.NET AJAX頁面方法接受並返回JSON,所以你需要你通過JSON.stringify發送到JSON數據轉換,就像這樣:

$(document).on("click", ".comments", function() { 
    var clicked = $(this); 
    var id = clicked.attr("data-id"); 
    alert(id); 
    var dataToSend = { 
     id: id 
    }; 
    $.ajax({ 
     type: "POST", 
     url: "Admin.aspx/ShowComments", 
     data: JSON.stringify(dataToSend), 
     dataType: "json", 
     contentType: "application/json; charset=utf-8", 
     success: function (data) { 
      var returnedstring = data.d; 
      alert(returnedstring); 
     } 
    }); 
    $('.pop-up-comments').addClass('show-popup'); 
});