【问题标题】:ASP.NET how to post updates during postback (I know how the postback works but still...)ASP.NET 如何在回发期间发布更新(我知道回发是如何工作的,但仍然......)
【发布时间】:2015-12-13 03:50:03
【问题描述】:

在我开始之前 - 我知道回发是如何工作的,我知道页面只有在完全呈现时才会更新,我只是想确保我的案例没有解决方案对页面进行小幅更新。

问题定义。我有 ASP.NET 项目和 WCF 服务。 WCF 服务包含一些函数,它们返回一些字符串作为结果(例如,是否有错误或进展顺利)。在 ASP.NET 网站上,我有一个按钮,可以触发一系列操作。这些操作是来自 WCF 服务的函数调用。使用通常的回发(它被称为我按下按钮的回发),只有在收到所有功能的结果时才会重新加载页面(这需要相当长的时间)。所有结果都添加到一个文本框中。

问题。有没有办法真正将结果异步添加到文本框?我的意思是,真的,使用 AJAX/其他东西,我不在乎。我不敢相信这个问题在 ASP.NET 中没有得到解决。我只需要用户查看进度 - 在整个序列被触发之前触发函数的结果。

我花了几个小时,除了 UpdatePanel 没有找到任何线索,但我无法用它来解决这个问题。你有什么想法吗?

protected void Button1_Click(object sender, EventArgs e)
{
  textBox1.text += wcf.function1();
  textBox1.text += wcf.function2();
  textBox1.text += wcf.function3();
  //only now the page updates.
}

【问题讨论】:

  • 您可以使用 AJAX 和 GenericHandler(*.ashx 文件)。从客户端,您通过 post 或 get 调用 .ashx。稍后(我现在正在做饭)我会发布一个示例

标签: c# asp.net .net wcf asp.net-ajax


【解决方案1】:

使用 ajax 和通用处理程序的演示。此示例是在 MonoDevelop 中制作的,但您可以在不更改代码的情况下传递给 Visual Studio。 文件夹和文件:

/*
DemoGenericHandler
     |
     |---Default.aspx
     |---Default.aspx.cs
     |
     |---GenericHandlers
     |         |
     |         |---MyHandler.ashx
     |         |---MyHandler.ashx.cs
     |
     |---web.config

*/

这是 Default.aspx 的代码

<%@ Page Language="C#" Inherits="DemoGenericHandler.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head runat="server">
	<title>Default</title>
	<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
	
	<script type="text/javascript">
	
	$(document).ready(function(){
		var $button1 = $("#<%= Button1.ClientID %>");
		var $txt1 = $("#<%= textBox1.ClientID %>");
		var $txt2 = $("#<%= textBox2.ClientID %>");
		var $txt3 = $("#<%= textBox3.ClientID %>");
		
		var $progressBar = $("#progressBar");
		
		$button1.click(function(e){
		
			//avoid postback
			e.preventDefault();
			
			//show progress bar
			$progressBar.fadeIn('fast');
			
			//ajax-post
			$.post("<%= ResolveClientUrl("~/") %>GenericHandlers/MyHandler.ashx", 
					{data:"requestFromDefaultPage"},
					function(jsonInstance){
						if(jsonInstance)
						{
							$txt1.val(jsonInstance.Value1);
							$txt2.val(jsonInstance.Value2);
							$txt3.val(jsonInstance.Value3);
						}
						
						//hide progressbar
						$progressBar.fadeOut('fast');
					
					});//ajax-post		
		});//click
		
	});
	</script>
</head>
<body>
	<form id="form1" runat="server">
		<asp:Button id="Button1" runat="server" Text="Call Ajax!" OnClick="Button1_Click" />	
		<img src="http://casa-vivebien.com/contents/media/progressbar.gif" id="progressBar" title="" style="display:none;" />
		<br />
		
		<asp:TextBox ID="textBox1" runat="server"></asp:TextBox>
		<asp:TextBox ID="textBox2" runat="server"></asp:TextBox>
		<asp:TextBox ID="textBox3" runat="server"></asp:TextBox>
		
	</form>
</body>
</html>

这是代码隐藏:

using System;
using System.Web;
using System.Web.UI;

namespace DemoGenericHandler
{
    public partial class Default : System.Web.UI.Page
    {

        protected void Button1_Click(object sender, EventArgs e)
        {
          //textBox1.Text += wcf.function1();
          //textBox1.Text += wcf.function2();
          //textBox1.Text += wcf.function3();
          //only now the page updates.
        }
    }
}

通用处理程序 (*.ashx.cs) 背后的代码:

using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Threading;

namespace DemoGenericHandler
{
    public class MyHandler : System.Web.IHttpHandler
    {

        public virtual bool IsReusable {
            get {
                return false;
            }
        }

        public virtual void ProcessRequest (HttpContext context)
        {
            //if you need get the value sent from client (ajax-post)
            //string valueSendByClient = context.Request.Form["data"] ?? string.Empty;

            //you must use a library like JSON.NET (newtonsoft) to serialize an object
            //here for simplicity i'll build the json object in a string variable:
            string jsonObj = "{\"Value1\": \"1\",\"Value2\":  \"2\",\"Value3\": \"3\"}";

            //await 5 seconds: (imitates the time that your wcf services take)
            Thread.Sleep(5000);

            //send the result to the client
            context.Response.ContentType = "text/json";
            context.Response.Write(jsonObj);
        }
    }
}

捕获:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多