2012-07-18 62 views
0

ASP.Net noob here尝试更新订阅事件的方法内的UpdatePanel内的控件,但没有运气。ASP.Net - 在引发事件时更新控制台内的更新面板

一些背景知识,我写了一个DLL接口与标准的POTS电话,我已经映射到手机端(电话被拿起,电话振铃等)到.Net事件的各种发生。

在我的ASP.Net解决方案中,我添加了我的DLL,实例化了我的手机,并且在订阅我的事件的方法中,我想更新UpdatePanel中的各种标签,将EventArgs对象中的信息传递到我的方法中。

使用断点,我可以看到我的电话对象正在按照预期工作,当它们应该和EventArgs包含他们应该的信息时引发事件。

但UpdatePanel内的标签永远不会更新。我将应用程序的先前版本作为Windows窗体编写,我记得无论何时从另一个线程更新UI,我都必须先检查InvokeRequired是否为真,如果是,则调用Invoke方法,但我不知道相当于这是在ASP.Net(如果有的话)。

标记低于(这只是我创建了让我周围的基础头实例项目):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> 

<!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> 
    <style type="text/css"> 
     #form1 
     { 
      text-align: center; 
     } 
    </style> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
     <ContentTemplate> 
      <asp:Label ID="Label1" runat="server" style="text-align: center" Text="Label"></asp:Label> 
      <br /> 
      <br /> 
      <asp:Button ID="Button1" runat="server" Text="Button" /> 

     </ContentTemplate> 
     <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="ScriptManager1" EventName="Load" /> 
     </Triggers> 
    </asp:UpdatePanel> 
    </form> 
</body> 
</html> 

现在的代码:

using System; 

//My DLL 
using Maximiser; 

namespace WebApplication2 
{ 
    public partial class WebForm1 : System.Web.UI.Page 
    { 
     //My Phone Object 
     private Phone _ThisPhone { get; set; } 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      //Instantiating Phone Object with IP Address and Port 
      _ThisPhone = new Phone("8.8.8.8", 8888); 

      //Hookstate event indicates phone is off/on the hook 
      _ThisPhone.HookState += new EventHandler<Maximiser.Events.Hookstate>(thisPhone_HookState); 
     } 

     void thisPhone_HookState(object sender, Maximiser.Events.Hookstate e) 
     { 
      //I want to update my Label with the phones current HookState 
      Label1.Text = e.State; 

      //Now I want to refresh the UpdatePanel but not reload the page 
      UpdatePanel1.Update(); 
     } 
    } 
} 

的方法肯定是运行如下图所示:

Breakpoint

回答

1

我输入你的更新面板和thisPhone_HookState方法放到我做的测试项目中,并且它工作正常。我开始怀疑你的方法是否被调用。你可以试着在你的方法中加一个断点来测试一下吗?

我想你会调用该方法,像这样

thisPhone_HookState(_ThisPhone.HookState, new EventHandler<Maximiser.Events.Hookstate>); 
+0

谢谢,但是该方法被运行时,我有一个截图 – JMK 2012-07-18 13:23:37

+0

出于好奇更新的问题,你能不能把这个在您的网页加载测试更新是否正常。 label1.text =“testing”; updatePanel1.Update(); – 2012-07-18 13:28:42

+0

那么,这工作正常,但是不是在同一个线程中调用,而我的事件运行在第二个线程,无法访问UI? – JMK 2012-07-18 13:31:31