网络方法无法阅读

时间:2015-03-27 13:40:32

标签: c# asp.net gridview

当我按下删除按钮删除数据库中的特定行时,我将其设为代码 当我点击按钮仅显示确认消息并且没有做任何事情时,删除功能没有读取 enter image description here 这是我的CS.aspx代码

<form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:BoundField DataField="Book_ID" HeaderText="Book_ID" HeaderStyle-Width="30" />
                <asp:BoundField DataField="Book_Name" HeaderText="Book_Name" HeaderStyle-Width="150" />
                <asp:BoundField DataField="Book_Author" HeaderText="Book_Author" HeaderStyle-Width="150" />
                <asp:BoundField DataField="Book_Description" HeaderText="Book_Description" HeaderStyle-Width="150" />
                <asp:TemplateField HeaderStyle-Width="50">
                    <ItemTemplate>
                        <asp:HiddenField ID="hfBook_ID" runat="server" Value='<%# Eval("Book_ID") %>' />
                        <asp:LinkButton ID="lnkDelete" Text="Delete" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("[id*=GridView1] [id*=lnkDelete]").click(function () {
                    if (confirm("Do you want to delete this Book?")) {
                        //Determine the GridView row within whose LinkButton was clicked.
                        var row = $(this).closest("tr");

                        //Look for the Hidden Field and fetch the Book_ID.
                        var book_ID = parseInt(row.find("[id*=hfBook_ID]").val());

                        //Make an AJAX call to server side and pass the fetched Book_ID.
                        $.ajax({
                            type: "GET",
                            url: "CS.aspx/DeleteBook",
                            data: '{book_ID: ' + book_ID + '}',
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (r) {
                                if (r.d) {
                                    //Remove the row from the GridView.
                                    row.remove();
                                    //If the GridView has no records then display no records message.
                                    if ($("[id*=GridView1] td").length == 0) {
                                        $("[id*=GridView1] tbody").append("<tr><td colspan = '4' align = 'center'>No records found.</td></tr>")
                                    }
                                    alert("Book record has been deleted.");
                                }
                            }
                        });
                    }
                    return false;
                });
            });
        </script>
    </form>

这是我的删除功能CS.aspx.cs

[WebMethod]
    public bool DeleteBook(int Book_ID)
    {

        using (con = new SqlConnection(conn))
        {
            using (SqlCommand cmd = new SqlCommand("DELETE FROM Book WHERE Book_ID = @Book_ID"))
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@Book_ID", Book_ID);
                con.Open();
                int rowsAffected = cmd.ExecuteNonQuery();
                con.Close();
                return rowsAffected > 0;
            }
        }
    }


问题是服务器端的DeleteBook()功能无法正常工作

1 个答案:

答案 0 :(得分:1)

我设法使用<input>字段的book_ID字段来使您的示例代码正常工作。我不得不解决几个问题。

<强>标记

<input type="text" value="234" id="fld" />
<input type="submit" id="submit" />
<script type="text/javascript">
    $(function () {
        $('#submit').click(function () {
            if (confirm('Do you want to delete this Book?')) {
                var book_ID = parseInt($('#fld').val());
                $.ajax({
                    type: "POST",
                    url: "callservice.aspx/DeleteBook",
                    data: '{Book_ID:' + book_ID + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (r) {
                    }
                });
            }
        });
    });
</script>

<强>代码隐藏

    [WebMethod]
    public static bool DeleteBook(int Book_ID)
    {
        return Book_ID.Equals(234);
    }
  • 使用POST作为类型
  • 重命名参数名称Book_ID以匹配CodeBehind中的方法参数
  • DeleteBook方法设为静态