2010-01-29 78 views
0

在處理ASP.NET中MessageQueue的ReceiveCompleted事件時遇到問題。 它成功捕獲它,但每次應用於頁面控件的更改都不起作用。ASP.NET和自定義事件的問題

這是我的本錢:

.ASPX

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False"> 
    <ContentTemplate> 
     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
     <br /> 
     <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> 
    </ContentTemplate> 
</asp:UpdatePanel> 

<asp:Timer ID="Timer1" runat="server" Interval="3000" ontick="Timer1_Tick"> 
</asp:Timer> 

.CS

private static System.Messaging.MessageQueue queue; 
private static String messageContent; 

protected override void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 
    queue = new MessageQueue(@".\Private$\MyQueue"); 
    queue.ReceiveCompleted += new ReceiveCompletedEventHandler(mq_ReceiveCompleted); 
    queue.BeginReceive(); 
} 


protected void mq_ReceiveCompleted(object sender, System.Messaging.ReceiveCompletedEventArgs e) 
{ 

    System.Messaging.Message message = queue.EndReceive(e.AsyncResult); 
    message.Formatter = new System.Messaging.XmlMessageFormatter(new string[] { "System.String,mscorlib" }); 

    Label1.Text = message.Body.ToString();  //Has no effect. The value updates without problem, but doesn't persist after finishing this method. And the Page doesn't refresh with this new value. 
    Label2.Text = DateTime.Now.ToString();  //Has no effect too. 
    Timer1.Interval = 99999;     //And this one the same, no effect. 
    messageContent = message.Body.ToString(); //.. But the value stored in this variable does persist 

    queue.BeginReceive(); 
} 

我不知道爲什麼它不能更新這些增值經銷商。這可能是任何無意義的,但我是ASP.NET的新手,所以任何線索都會受到歡迎。

在此先感謝!

巴勃羅

+0

您是否有UpdateManager的ScriptManager? – curtisk 2010-01-29 16:49:58

+0

是的,我喜歡這個: \t ... Pablo 2010-01-30 15:56:40

回答

1

你希望客戶端頁面到命令(由mq_ReceiveCompleted)從服務器進行更新,對不對?如果是這樣,這是不可能的。

我的建議是把客戶端的JS功能,將通過定時器調用(每秒左右),並會發送一個異步AJAX請求在的MessageQueue新郵件的Web服務。如果存在這樣的消息,JS將採取任何所需的操作(更新頁面等)

+0

是的,這是我的意圖......我認爲有可能更新從服務器的頁面,但我看到我在做一個誤解。 感謝您的幫助! – Pablo 2010-02-01 21:03:51

0

嘗試設置的UpdateMode = 「始終」到您的UpdatePanel,或撥打UpdatePanel1.Update();mq_ReceiveCompleted的端部()方法。

+0

謝謝,但沒有成功都嘗試。 – Pablo 2010-01-30 15:54:48

0

檢查您是否正在更新頁面對象的正確實例。

+0

我該如何檢查? 我的意思是,只是用這個: Label1.Text = m.Body.ToString(); 頁面應該更新自己,不應該嗎? 無論如何,我已經嘗試添加UpdatePanel1.Update()作爲GregoryM建議,並且它不起作用。 – Pablo 2010-01-30 16:02:15