webform ajax 异步请求

本文介绍ASP.NET WebForm中使用AJAX实现局部刷新的方法,并通过两个示例展示如何不刷新整个页面仅更新部分内容。第一个示例展示了如何通过调用WebMethod方法SayHello返回字符串并显示。第二个示例则演示了如何通过发送参数并在服务器端处理这些参数来更新页面内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


第一种就是对应方法的请求 虽然对应方法 但还是会刷新页面 webform是基于事件的 每次请求都会出发pageload
<script>
        $(function () {
            $("#btn").click(function () {
                $.ajax({
                    type: "POST",
                    contentType: "application/json",
                    url: "Index.aspx/SayHello",
                    data: null,
                    dataType: "json",
                    success: function (msg) {
                        alert(msg.d);
                    }
                });
            });
        });
    </script>




[System.Web.Services.WebMethod] public static string SayHello() { return "Hello"; }


第二种方法 就是直接请求 pageload
protected void Page_Load(object sender, EventArgs e)
{
  _delWhere = "";
  if (Request["where"] != null)
  {

    _where = Request["where"].ToString();
    BindData(_where);
  }
}

  

 $.ajax({
            url: "BugListnew.aspx?where=" + where,
            type: "Post",
            dataType: "json", //请求到服务器返回的数据类型  
            data: { "where": where },

            success: function(data) {

                var obj = $.parseJSON(data); //这个数据  

                var name = obj.Json[0].UserName;
                var age = obj.Json[0].Age;

                document.getElementById("name").innerHTML = name;
                document.getElementById("age").innerHTML = age;
            }

        })

  

转载于:https://www.cnblogs.com/tony-brook/p/7874195.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值