简体   繁体   English

轻松异步回调替代

[英]Easy Async Callback Alternative

I need help with my logic... 我的逻辑需要帮助...

Currently: 目前:

  1. A user inputs some data in a page from. 用户在页面中输入一些数据。
  2. The user clicks submit. 用户单击提交。
  3. Javascript places their data in a hidden input field. Javascript将其数据放在隐藏的输入字段中。
  4. The page does a postback (reloads) 页面执行回发(重新加载)
  5. C# registers that this is a postback and gets the hidden javascript field. C#注册这是一个回发并获取隐藏的javascript字段。
  6. C# puts it in the database. C#将其放入数据库中。

The problem with the above is the reloading of the webpage. 上面的问题是网页的重新加载。 I do not want that. 我不要那个。 I have played around with a callback script that is not working (I have another question open on trying to debug that). 我玩了一个不起作用的回调脚本(尝试调试该脚本时还有另一个问题)。

My question is about alternatives to accomplish my steps as defined above without a callback or postback? 我的问题是关于在没有回调或回发的情况下完成上述步骤的替代方案? Is there a way to simply call the server-side function on demand from a client side function? 有没有一种方法可以根据需要从客户端函数简单地调用服务器端函数? (I know callbacks should do this very thing, but mine is not working) (我知道回调应该做这个事情,但是我的工作不正常)

If there is no such alternative, why is it that callbacks are so complicated (from a novice's point of view)? 如果没有其他选择,为什么回调是如此复杂(从新手的角度来看)? Why can I not simply call a callback like: 为什么不能简单地像这样调用回调:

if (IsCallBack)
        {
            string test = Request.Form["saveTest"];
            //do stuff
        }

Thank you. 谢谢。 Also, please do not 'close' or mark down my question without first critiquing me. 另外,请先“批评”我,然后再“关闭”或写下我的问题。 Thanks. 谢谢。

Here's the simplest example I can think of, that uses Ajax with ASP.Net. 这是我能想到的最简单的示例,该示例将Ajax与ASP.Net结合使用。

  • using JQuery, because it simplifies making the Ajax request 使用JQuery,因为它简化了Ajax请求
  • not using ASP.Net AJAX, because it adds abstraction on top of the core technology, and that is not a good way to start learning 不使用ASP.Net AJAX,因为它在核心技术之上添加了抽象,而这并不是开始学习的好方法

First, UserInput.aspx 首先,UserInput.aspx

<html>
    <input type='text' id='SomeUserInput' />
    <input type='submit' value='submit' id='SubmitButton' />

    <script src='http://code.jquery.com/jquery-1.10.2.js'></script>
    <script>  
        // wait for the document to load
        $(document).ready(function() {

            // handle the button click
            $("#SubmitButton").on("click", function(e) {

                // stop the page from submitting, so we can make the Ajax call
                e.preventDefault();

                // retrieve the user input
                var userInput = $("#SomeUserInput").val();

                // make the Ajax request
                $.post("AjaxHandler.aspx", { saveText: userInput }, function(response) {

                    // handle response from the server
                    alert("Request is complete.  Result: " + response);
                });
            });
        });
    </script>
</html>

Second, AjaxHandler.aspx 二,AjaxHandler.aspx

<%@ Page Language="C#" %>

<%
    string userInput = Request.Params["saveText"];

    // use the input here

    Response.Write("done, maybe?");
%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM