葡萄酸

快乐会传染,我从不吝啬

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

统计

.net后台获取Confirm返回值

日前做到一个需求.net后台获取js返回值,根据前台获取yes or no,后台执行不同业务逻辑。

解决方案:后台业务逻辑写入第三方页面,前台脚本使用JQ Ajax请求。

DEMO:点击按钮弹出Confirm,Sure加法运算,Cancel减法运算,运算逻辑写于第三方页面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo_ReturnConfirm.aspx.cs" Inherits="ClientTest.Demo_ReturnConfirm" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="js/jquery-2.1.0.js"></script>
    <script type="text/javascript">
        function operate() {
            var p_one = $("#txtOne").val();
            var p_two = $("#txtTwo").val();
            var a = confirm("点击确定加法运算,取消减法运算");
            if (a == true) {
                $.ajax({
                    type: "Get", //访问WebService使用Post方式请求
                    url: "index.aspx", //调用WebService
                    data: 'type=Add&p_one=' + p_one + '&p_two=' + p_two,
                    dataType: 'html',
                    // cache: false,
                    //beforeSend: function (x) { x.setRequestHeader("Content-Type", "application/json; charset=utf-8"); },
                    error: function (x, e) { alert("Error:" + x + e) },
                    success: function (result) { //回调函数,result,返回值
                        alert(result);
 
                    }
                });
            }
            else {
                $.ajax({
                    type: "Get", //访问WebService使用Post方式请求
                    url: "index.aspx", //调用WebService
                    data: 'type=Cut&p_one=' + p_one + '&p_two=' + p_two,
                    dataType: 'html',
                    //cache: false,
                    //beforeSend: function (x) { x.setRequestHeader("Content-Type", "application/json; charset=utf-8"); },
                    error: function (x, e) { alert("Error:" + x + e) },
                    success: function (result) { //回调函数,result,返回值
                        alert(result);
                    }
                });
            }
            return false;
        };
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtOne" runat="server"></asp:TextBox>
            <asp:TextBox ID="txtTwo" runat="server"></asp:TextBox>
            <asp:Button ID="BtnSure" runat="server" Text="Operate" OnClientClick=" return operate()" />
        </div>
    </form>
</body>
</html>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public partial class Index : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          if (Request.QueryString["type"] == "Add" && Request.QueryString["p_one"] != null && Request.QueryString["p_two"] != null)
          {
              Response.Write(Operate_Add(Convert.ToInt32(Request.QueryString["p_one"].ToString()), Convert.ToInt32(Request.QueryString["p_two"].ToString())));
              Response.End();
          }
          if (Request.QueryString["type"] == "Cut" && Request.QueryString["p_one"] != null && Request.QueryString["p_two"] != null)
          {
              Response.Write(Operate_Cut(Convert.ToInt32(Request.QueryString["p_one"].ToString()), Convert.ToInt32(Request.QueryString["p_two"].ToString())));
              Response.End();
          }
      } <br>        private string Operate_Add(int p_one, int p_two)
      {
          return (p_one + p_two).ToString();
 
 
      }
      private string Operate_Cut(int p_one, int p_two)
      {
          return (p_one - p_two).ToString();
      }
 
  }

 

posted on   虞筱瑶  阅读(794)  评论(0编辑  收藏  举报

编辑推荐:
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· SQL Server统计信息更新会被阻塞或引起会话阻塞吗?
· C# 深度学习框架 TorchSharp 原生训练模型和图像识别
阅读排行:
· 这或许是全网最全的 DeepSeek 使用指南,95% 的人都不知道的使用技巧(建议收藏)
· 拒绝繁忙!免费使用 deepseek-r1:671B 参数满血模型
· 本地搭建DeepSeek和知识库 Dify做智能体Agent(推荐)
· Sdcb Chats 重磅更新:深度集成 DeepSeek-R1,思维链让 AI 更透明!
· DeepSeek-R1本地部署如何选择适合你的版本?看这里
点击右上角即可分享
微信分享提示