簡體   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