ASP.NET 和自定义事件的问题
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您希望通过来自服务器的命令(由 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.)
尝试将 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.检查您是否正在更新页面对象的正确实例。
Check that you are updating the correct instance of the page object.