[英]ASP.NET how to post updates during postback (I know how the postback works but still…)
在開始之前 -我知道回發的工作原理,我知道頁面只有在完全呈現后才會更新,我只想確保沒有針對我的情況對頁面進行次要更新的解決方案。
問題定義 。 我有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和通用處理程序的演示。 這個例子是在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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.