ASP.NET之通过JS向服务端(后台)发出请求(__doPostBack is undefined)

ASP.NET回发数据是通过函数__doPostBack来实现的,该函数在添加了服务端控件,并将AutoPostBack设置为true之后,将自动生成,具体可以参看下面的图。






同时还会生成隐藏控件,其ID为__EVENTTARGET和__EVENTARGUMENT,前一个是用于存放key的,后一个用于存放参数的。

所以在后台通过Request.Form来获取所要的数据,test.aspx.cs代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ExampleTest
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String key = Request.Form["__EVENTTARGET"];
            if (!String.IsNullOrWhiteSpace(key))
            {
                String value = Request.Form["__EVENTARGUMENT"];              
                String info = "Key=" + key + " Value=" + value;
                Response.Write("<script type=\"text/javascript\">alert('" + info + "');</script>");

                TextBox1.Text = info;
            }      
   
            
        }
    }
}

注:

对于Button和ImageButton会有不一样,可以参考下面的文章
http://blog.csdn.net/luxuejuncarl/article/details/1479226
http://www.cnblogs.com/hjf1223/archive/2006/07/05/443761.html



效果图:

附前端代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="ExampleTest.test" %>

<!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 type="text/javascript">
        function test() {
            __doPostBack("AA", "111");
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input id="btnTest" type="button" οnclick="test();" value="test" />

            <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true"></asp:TextBox>
        </div>

    </form>
</body>
</html>



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值