繁体   English   中英

从Javascript调用C#方法并在

[英]Calling a C# method from Javascript and executing the code inside the

当用户单击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中选择的选项执行此代码? 谢谢您的帮助

您需要使用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#方法的路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM