2012-07-18 37 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