ASP.NET 和自定义事件的问题

发布于 2024-08-19 06:40:00 字数 1848 浏览 15 评论 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 的新手,所以任何线索都将受到欢迎。

提前致谢!

巴勃罗

I have a problem when handling the ReceiveCompleted event of a MessageQueue in ASP.NET.
It catches it successfully, but every changes applied to the Controls of the page have no effect.

This is what I've got:

.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();
}

I don't why it fails updating those vars. It may be any nonesense, but I'm new to ASP.NET, so any clue will be welcome.

Thanks in advance!

Pablo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轻许诺言 2024-08-26 06:40:00

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

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

You want the client page to be updated by the command (caused by mq_ReceiveCompleted) from the server, right? It isn't possible if it's so.

My suggestion is to put a client JS function that will be called by timer (each second or so) and will send an async AJAX request to the web service for new messages in MessageQueue. If such message exists the JS will take any actions needed (updating the page, etc.)

清风疏影 2024-08-26 06:40:00

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

Try setting the UpdateMode="Always" to your UpdatePanel, or call UpdatePanel1.Update(); at the end of mq_ReceiveCompleted() method.

少年亿悲伤 2024-08-26 06:40:00

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

Check that you are updating the correct instance of the page object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文