显示来自C#代码的警报

显示来自C#代码的警报,c#,ajax,webforms,C#,Ajax,Webforms,这是一个WebMethod,它从lvl字符串的前端获取值。 稍后,如果数据库中已经存在该值,则通过getDuplicate过程检查该字符串。如果该值存在,则不会激活insert porecedure InsertObject,如果数据库中没有该值,则frist过程返回null,insert过程将工作 代码中的一切都很好,如果插入成功,或者插入失败,我只需要从代码的C#部分发出某种警报消息。 我尝试了太多的例子,但找不到任何解决方案:/ 有人能帮忙吗 [WebMethod(EnableSessio

这是一个WebMethod,它从lvl字符串的前端获取值。 稍后,如果数据库中已经存在该值,则通过getDuplicate过程检查该字符串。如果该值存在,则不会激活insert porecedure InsertObject,如果数据库中没有该值,则frist过程返回null,insert过程将工作

代码中的一切都很好,如果插入成功,或者插入失败,我只需要从代码的C#部分发出某种警报消息。 我尝试了太多的例子,但找不到任何解决方案:/ 有人能帮忙吗

[WebMethod(EnableSession = true)]
        public static void GetCollection(string lvl)
        {

    string conn = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(conn))

        try
        {

            connection.Open();
            SqlCommand cmdCount = new SqlCommand("getDuplicate", connection);
            cmdCount.CommandType = CommandType.StoredProcedure;
            cmdCount.Parameters.AddWithValue("@ObjekatName", lvl);
            var count = (string)cmdCount.ExecuteScalar();


            if (count == null)
            {
                SqlCommand cmdProc = new SqlCommand("InsertObjekat", connection);
                cmdProc.CommandType = CommandType.StoredProcedure;
                cmdProc.Parameters.AddWithValue("@ObjekatName", lvl);                   
                cmdProc.ExecuteNonQuery();
              //successful alert
            }
            else
            {
              //fail alert
            }
        }


        catch 
        {


        }
        finally
        {
            connection.Close();

        }

    return;

    {
}
更新: 向方法发送值的Ajax:

$(function () {

             $('#myButton').on('click', function () {

                 var lvl = $('#MainContent_txtProductConstruction').val()

                 $.ajax({
                     type: "POST",
                     url: "NewProductConstruction.aspx/GetCollection",

                     data: JSON.stringify({'lvl': lvl }),

                     contentType: "application/json; charset=utf-8",
                     dataType: "json",

                     success: function (response) {
                         alert("Saved successfully.");
                         console.log(response);
                         location.reload(true);

                     },
                     error: function (response) {
                         alert("Not Saved!");
                         console.log(response);
                         location.reload(true);
                     }

                 });

             });

         });

更改webmethod返回类型

[WebMethod(EnableSession = true)]
public static int GetCollection(string lvl)
{
int success=0;
string conn = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
using (SqlConnection connection = new SqlConnection(conn))

    try
    {

        connection.Open();
        SqlCommand cmdCount = new SqlCommand("getDuplicate", connection);
        cmdCount.CommandType = CommandType.StoredProcedure;
        cmdCount.Parameters.AddWithValue("@ObjekatName", lvl);
        var count = (string)cmdCount.ExecuteScalar();


        if (count == null)
        {
            SqlCommand cmdProc = new SqlCommand("InsertObjekat", connection);
            cmdProc.CommandType = CommandType.StoredProcedure;
            cmdProc.Parameters.AddWithValue("@ObjekatName", lvl);                   
            cmdProc.ExecuteNonQuery();
            success= 1;
        }
        else
        {

        }
    }


    catch 
    {


    }
    finally
    {
        connection.Close();

    }

return success;

}
}

在Jquery代码中做一些小改动

       $(function () {

         $('#myButton').on('click', function () {

             var lvl = $('#MainContent_txtProductConstruction').val()

             $.ajax({
                 type: "POST",
                 url: "NewProductConstruction.aspx/GetCollection",

                 data: JSON.stringify({'lvl': lvl }),

                 contentType: "application/json; charset=utf-8",
                 dataType: "json",

                 success: function (response) {
                     if(parseInt(response.d)>0)
                        alert("Saved successfully.");
                     else
                        alert("Not Saved!");
                     console.log(response);
                     location.reload(true);

                 },
                 error: function (response) {
                     alert("Not Saved!");
                     console.log(response);
                     location.reload(true);
                 }

             });

         });

     });

我猜您必须返回某种JSON结构,并在其上显示响应,然后通过使用javascript解释响应并显示警报,在web端向用户显示该信息。一个
WebMethod
应该返回客户端代码(在您的例子中是一些javascript警报)。它不能返回
void
@HimBromBeere一个名为GetSomething的
WebMethod
应该返回某物;-)是否有某种方式可以直接从C#发送JavaScirpt警报,或者类似的警报?这决定了webmethod应该做什么。如果它返回一个字符串,您可以只使用
返回“alert(…)”
。很抱歉,此示例不起作用:/message未显示在前端,并且页面在成功后不会重新加载。请将parsInt更改为parsInt。然后再试一次。语法问题非常好!谢谢你!:)