0

在 ASP.NET 中处理 MessageQueue 的 ReceiveCompleted 事件时遇到问题。它成功捕获它,但是应用于页面控件的每个更改都没有效果。

这就是我所拥有的:

.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 的新手,因此欢迎提供任何线索。

提前致谢!

巴勃罗

4

3 回答 3

1

您希望通过来自服务器的命令(由 mq_ReceiveCompleted 引起)更新客户端页面,对吗?如果是这样,那是不可能的。

我的建议是放置一个客户端 JS 函数,该函数将由计时器(每秒左右)调用,并将向 Web 服务发送异步 AJAX 请求以获取 MessageQueue 中的新消息。如果存在这样的消息,JS 将采取任何需要的操作(更新页面等)

于 2010-02-01T14:19:48.597 回答
0

尝试将UpdateMode="Always"设置为您的 UpdatePanel,或在mq_ReceiveCompleted()方法UpdatePanel1.Update();的末尾调用。

于 2010-01-29T16:54:32.440 回答
0

检查您是否正在更新页面对象的正确实例。

于 2010-01-29T17:00:30.230 回答