【问题标题】:Show alert from C# code显示来自 C# 代码的警报
【发布时间】:2018-07-05 12:31:56
【问题描述】:

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

代码中的一切都运行良好,如果插入成功,我需要的只是来自代码的 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);
                     }

                 });

             });

         });

【问题讨论】:

  • 我猜你必须返回某种 JSON 结构和你的响应,并设法通过使用 javascript 来解释响应并显示警报来向 Web 端的用户显示该信息.
  • A WebMethod 应该返回客户端代码(在您的情况下是一些 javascript-alert)。它不能返回void
  • @HimBromBeere A WebMethod 名为 GetSomething 应该返回一些东西 ;-)
  • 是否有某种方法可以直接从 C# 发送 JavaScirpt 警报,或某种类似的警报?
  • 这取决于您的网络方法应该做什么。如果它返回一个字符串,你可以使用return "alert(...)"

标签: c# ajax webforms


【解决方案1】:

更改 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);
                 }

             });

         });

     });

【讨论】:

  • 对不起,这个例子不起作用:/按摩没有显示在前端,成功后页面没有重新加载。
  • 将 parsInt 更改为 parseInt。然后再试一次。语法问题
  • 完美运行!谢谢分配! :)
猜你喜欢
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多