ASP.NET之通過JS向服務端(後台)發出請求(__doPostBack is undefined)

來源:互聯網
上載者:User

標籤:asp.net   __dopostback   

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" onclick="test();" value="test" />            <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true"></asp:TextBox>        </div>    </form></body></html>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.