ASP.NET 4.0 Web表单 - 更新3板异步 [英] ASP.NET 4.0 Webforms - Update 3 panels asynchronously

查看:192
本文介绍了ASP.NET 4.0 Web表单 - 更新3板异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

梅比我想的这个错误的方式,我知道页面呈现,并发送到客户端(网页浏览器)。但我真的需要明白这一点。


我如何异步更新我的三个UpdatePanel的?为了模拟此我为环路第二每次补充说,正确的数字是发现和触发事件后创建一个。
因此,我想要做的是能在web浏览器看到,当这些更新完成。
时间跨度会是这样的:

时间结果
0,0235(只是一个数字toshow真的发生快速)结果
在页面上会发生什么?
几乎瞬间第一个标签获取与7号更新
时间结果
0,0235加一个秒= 1,0235
在页面上会发生什么?
几乎瞬间第二个标签获取与8号更新
时间结果
0,0235加一个秒= 2,0235
在页面上会发生什么?
几乎瞬间第三标签获取与9号更新

我ahve造访的页面,首先告诉你一些文本和一个几秒钟,他们告诉你一个图后,当请求已完成加载?所以,必须有一个办法分辩?

我的继承人code,我没有张贴我CounterEventArgs类它的工作原理,并不会影响这个问题。

下面是我的Index.aspx - codebehind
公共部分类_Default:System.Web.UI.Page
{
    私人的TestClass _testet;

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    ScriptManager1.RegisterAsyncPostBackControl(Button1的);
}
无效_testet_CounterFoundNumber(对象发件人,CounterEventArgs E)
{
    开关(e.labelnumber)
    {
        情况1:
            Label1.Text = e.positionen.ToString();
            打破;
        案例2:
            Label2.Text = e.positionen.ToString();
            打破;
        案例3:
            Label3.Text = e.positionen.ToString();
            打破;
    }
}
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
    TestClass中testet =新识别TestClass();
    _testet = testet;
    _testet.CounterFoundNumber + =新testClass.CounterEventArgsHandler(_testet_CounterFoundNumber);
    _testet.count();
}
}

和源

 <%@页面语言=C#AutoEventWireup =真codeFILE =Default.aspx.cs继承=_默认异步=真正的% >  !< D​​OCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> < HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
 <头=服务器>
<标题>< /标题>
< /头>
<身体GT;
  <表ID =form1的=服务器>
  < D​​IV>
    < ASP:按钮的ID =Button1的=服务器的onclick =的button1_Click文本=按钮/>
< / DIV>
< ASP:的ScriptManager ID =ScriptManager1=服务器的EnablePartialRendering =真>
< / ASP:ScriptManager的>
< ASP:的UpdatePanel ID =updatepanel1=服务器的UpdateMode =始终>
<&的ContentTemplate GT;
< ASP:标签ID =Label1的=服务器文本=标签>< / ASP:标签>
< /表及GT;
< /&的ContentTemplate GT;
< / ASP:的UpdatePanel> < ASP:的UpdatePanel ID =updatepanel2=服务器的UpdateMode =始终>
<&的ContentTemplate GT;
  < ASP:标签ID =Label2的=服务器文本=标签>< / ASP:标签>
< /表及GT;
< /&的ContentTemplate GT;
< / ASP:的UpdatePanel>
 < ASP:的UpdatePanel ID =updatepanel3=服务器ChildrenAsTriggers =假的UpdateMode =条件>
<&的ContentTemplate GT;


    
    
    
    

和最后我的TestClass

 公共类识别TestClass
{
公共事件CounterEventArgsHandler CounterFoundNumber;
公共委托无效CounterEventArgsHandler(对象发件人,CounterEventArgs E);
公众诠释Y = 1;    受保护的虚拟无效OnCounterFoundNumber(CounterEventArgs E)
    {
        如果(CounterFoundNumber!= NULL)
        CounterFoundNumber.BeginInvoke(这一点,即,新的AsyncCallback(this.ResultFoundNumber),NULL);
    }    公共无效计数()
    {
        的for(int i = 0;我小于10;我++)
        {
            如果(ⅰ> = 7)
            {
               OnCounterFoundNumber(新CounterEventArgs(I,y)的);
               ÿ++;
               System.Threading.Thread.Sleep(1000);
            }
        }
    }
    公共无效ResultFoundNumber(IAsyncResult的AR)
    {
        CounterFoundNumber.EndInvoke(AR);
    }
}


解决方案

而不是使用Javascript在code写尝试背后的 的setInterval()的方法

在JavaScript函数的引发一些按钮点击事件,做一个局部邮用你和更新面板。

如果你愿意,你可以通过设置CSS属性使按钮为不可见的 DISPLY:无

或者请检查落实 COMET在ASP.Net 或< STRONG> 的WebSockets 的执行,如果你使用HTML 5。

希望这会帮助你!

Maybee I'm thinking of this the wrong way, I know the page renders and is sent to the client(webbrowser). But I really need to get to understand this.

Question How do I update my three updatepanels asynchronously? To simulate this I have created a for loop which adds a second each time, after correct number is found and event is fired. So what I want to do is to be able to see in the webbrowser, when these updates are done. The time span would be like this:

TIME
0,0235(just a number toshow it really happens quick)
What happens on the page? Almost instantly first label get's updated with number 7 TIME
0,0235 + one second = 1,0235 What happens on the page? Almost instantly second label get's updated with number 8 TIME
0,0235 + one second = 2,0235 What happens on the page? Almost instantly third label get's updated with number 9

I ahve visted pages that first show you some text and after a couple of seconds they show you a diagram, when that request has finished loading? So there must be a way rigth?

Heres my code, I'm not posting my CounterEventArgs class it works and doesn't affect the question.

Here's my index.aspx - codebehind public partial class _Default : System.Web.UI.Page { private testClass _testet;

protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager1.RegisterAsyncPostBackControl(Button1);  
}
void _testet_CounterFoundNumber(object sender, CounterEventArgs e)
{
    switch (e.labelnumber)
    {
        case 1:
            Label1.Text = e.positionen.ToString();            
            break;
        case 2:
            Label2.Text = e.positionen.ToString();        
            break;
        case 3:
            Label3.Text = e.positionen.ToString();        
            break;
    } 
}
protected void Button1_Click(object sender, EventArgs e)
{
    testClass testet = new testClass();
    _testet = testet;
    _testet.CounterFoundNumber += new testClass.CounterEventArgsHandler(_testet_CounterFoundNumber);
    _testet.count();
}
}

And the Source

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Async="true"%>

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
<title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>  
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<asp:UpdatePanel ID="updatepanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</ContentTemplate>
</asp:UpdatePanel>

 <asp:UpdatePanel ID="updatepanel2" runat="server" UpdateMode="Always">
<ContentTemplate>
  <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</form>
</ContentTemplate>
</asp:UpdatePanel>
 <asp:UpdatePanel ID="updatepanel3" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>

And lastly my testclass

public class testClass
{
public event CounterEventArgsHandler CounterFoundNumber; 
public delegate void CounterEventArgsHandler(object sender, CounterEventArgs e);
public int y = 1;

    protected virtual void OnCounterFoundNumber(CounterEventArgs e)
    {
        if (CounterFoundNumber != null)
        CounterFoundNumber.BeginInvoke(this, e, new AsyncCallback(this.ResultFoundNumber), null);
    }

    public void count()
    {          
        for (int i = 0; i < 10; i++)
        {
            if (i >= 7)
            {    
               OnCounterFoundNumber(new CounterEventArgs(i,y));
               y++;
               System.Threading.Thread.Sleep(1000);
            }              
        }
    }
    public void ResultFoundNumber(IAsyncResult ar)
    {
        CounterFoundNumber.EndInvoke(ar);
    }
}

解决方案

Instead of writing in code behind try using Javascript setInterval method()

In a JavaScript function trigger some button click event that does a partial postpack for you and update the panels.

If you want you can make the button as invisible by setting css property disply:none

Or please check implementing COMET in ASP.Net or WebSockets implementation if you use HTML 5.

Hope this would help you!

这篇关于ASP.NET 4.0 Web表单 - 更新3板异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆