从Javascript调用C#方法并在

时间:2018-09-25 14:57:49

标签: javascript c# asp.net

当用户从GridView单击OnRowDeleting调用时,我具有从C#代码调用的Javascript函数。这是Call和方法

 OnRowDeleting="GridView1_RowDeleting"
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {  
       ClientScript.RegisterStartupScript(GetType(), "hwa", "Ealert();", true);       
    }

然后调用该JS代码,并向用户询问问题。基于它们是否命中。我需要JS将调用发送到C#方法。这是应该调用的JS代码和C#代码。

<script> ...JS..
    function Ealert() {
            //var test = document.getElementById("hdField");
            bootbox.confirm({
            message: "Did you send the renewal email associated with this client?",
            buttons: {
                confirm: {
                    label: 'Yes',
                    className: 'btn-success'
                },
                cancel: {
                    label: 'No',
                    className: 'btn-danger'
                }
            },
                callback: function (result) {
                    if (result == true) {
                        bootbox.alert({
                            message: "Thank you",
                            size: 'medium'
                        });
                      // document.getElementById('<%=hdField.ClientID %>').value = "true"; 
                    } else {
                            bootbox.alert({
                            message: "Please go back to your saved location\n and send the renewal email.",
                            size:'small'
                        });
                        // document.getElementById('<%= hdField.ClientID %>').value = "false";
                        PageMethods.testCSharp();
                        function onSuccess(result) {
                        alert(result);
                         }

                         function onFailure(result) {
                         alert("Failed!");
                        }

                    }                        
                    console.log('This was logged in the callback: ' + result);

                }          
        });
    }     

C#

 [WebMethod]
    public static void testCSharp(bool result, GridView GridView1, object sender, EventArgs e)
    {
        bool variable = result;
        string t = result.ToString();

        MessageBox.Show(t);

        string UniqClient = GridView1.SelectedRow.Cells[1].Text;
        string UniqPolicy = GridView1.SelectedRow.Cells[3].Text;
        string emailed = "No";
        string query = "UPDATE [Reviewed_Renewal_Policy] SET [Emailed] = @emailed where where ([UniqClient] = @UniqClient) AND ([UniqPolicy] = @UniqPolicy)";

        using (SqlConnection conn = new SqlConnection("Data Source=GTU-BDE01;Initial Catalog=GTU_Apps;Integrated Security=True"))
        {
            using (SqlCommand comm = new SqlCommand(query, conn))
            {
                comm.Parameters.AddWithValue("@UniqClient", UniqClient);
                comm.Parameters.AddWithValue("@UniqPolicy", UniqPolicy);
                comm.Parameters.AddWithValue("@emailed", emailed);
                conn.Open();
                comm.ExecuteNonQuery();
                conn.Close();
            }
        }


        return;

    }

问题在于此方法的PageMethods调用永远无法正常工作。我收到错误消息,说PageMethods的安装不正确,但是将方法更改为public static void可以解决此问题。但是,它在方法本身中什么也不执行。

我已注释掉SQL查询,并使用MessageBox.Show作为测试,但它不起作用。有谁知道为什么或如何根据JavaScript中选择的选项执行此代码?感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您需要使用Ajax调用才能将值发送到C#函数。

    $.ajax({
    data: formData,
    method: "Post",
    url: url,
    processData: false,
    contentType: false,
    success: function (d) {
       SuccessMessage(successMsg);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        ErrorMessage(errorMsg);
    }
});

与此类似。 URL将是您的c#方法的路径。