1

我想在表单上添加进度条。我有一个向几个用户发送邀请电子邮件的表格。那么我怎样才能让进度条显示发送电子邮件的过程呢?

我在.aspx文件中添加以下代码

<asp:UpdateProgress ID="UpdateProgress" runat="server">
 <ProgressTemplate>
 <asp:Image ID="Image1" ImageUrl="~/ProgressImage/ajax-loader.gif" AlternateText="Processing" runat="server" />
 </ProgressTemplate>
</asp:UpdateProgress>

这种方法正确吗?

4

3 回答 3

3

对我来说看起来不错...您在这个 aspx 页面上也有一个scriptManager和一个updatePanel吗?请参阅此演示代码。

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Button ID="btnSendMail" runat="server" Text="Send Mail" OnClick="btnSendMail_Click" />
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
    <ProgressTemplate>
        Sending Mail
    </ProgressTemplate>
</asp:UpdateProgress>
于 2012-09-03T11:19:01.860 回答
1

使用推送技术:我是这样做的:Class with message

 public class Message
    {
        /// <summary>
        /// The name who will receive this message.
        /// </summary>
        public string RecipientName { get; set; }

        /// <summary>
        /// The message content.
        /// </summary>
        public string MessageContent { get; set; }
    }

代表客户的类:

public class Client
    {
        private ManualResetEvent messageEvent = new ManualResetEvent(false);
        private Queue<Message> messageQueue = new Queue<Message>();

        /// <summary>
        /// This method is called by a sender to send a message to this client.
        /// </summary>
        /// <param name="message">the new message</param>
        public void EnqueueMessage(Message message)
        {
            lock (messageQueue)
            {
                messageQueue.Enqueue(message);

                // Set a new message event.
                messageEvent.Set();
            }
        }

        /// <summary>
        /// This method is called by the client to receive messages from the message queue.
        /// If no message, it will wait until a new message is inserted.
        /// </summary>
        /// <returns>the unread message</returns>
        public Message DequeueMessage()
        {
            // Wait until a new message.
            messageEvent.WaitOne();

            lock (messageQueue)
            {
                if (messageQueue.Count == 1)
                {
                    messageEvent.Reset();
                }
                return messageQueue.Dequeue();
            }
        }
    }

向客户端发送消息的类:

public class ClientAdapter
    {
        /// <summary>
        /// The recipient list.
        /// </summary>
        private Dictionary<string, Client> recipients = new Dictionary<string,Client>();

        /// <summary>
        /// Send a message to a particular recipient.
        /// </summary>
        public void SendMessage(Message message)
        {
            if (recipients.ContainsKey(message.RecipientName))
            {
                Client client = recipients[message.RecipientName];

                client.EnqueueMessage(message);
            }
        }

        /// <summary>
        /// Called by a individual recipient to wait and receive a message.
        /// </summary>
        /// <returns>The message content</returns>
        public string GetMessage(string userName)
        {
            string messageContent = string.Empty;

            if (recipients.ContainsKey(userName))
            {
                Client client = recipients[userName];

                messageContent = client.DequeueMessage().MessageContent;
            }

            return messageContent;
        }

        /// <summary>
        /// Join a user to the recipient list.
        /// </summary>
        public void Join(string userName)
        {
            recipients[userName] = new Client();
        }

        /// <summary>
        /// Singleton pattern.
        /// This pattern will ensure there is only one instance of this class in the system.
        /// </summary>
        public static ClientAdapter Instance = new ClientAdapter();
        private ClientAdapter() { }
    }

发送消息:

 Message message = new Message
                                      {
                                          RecipientName = tbRecipientName.Text.Trim(),
                                          MessageContent = tbMessageContent.Text.Trim()
                                      };

                if (!string.IsNullOrWhiteSpace(message.RecipientName) && !string.IsNullOrEmpty(message.MessageContent))
                {
                    // Call the client adapter to send the message to the particular recipient instantly.
                    ClientAdapter.Instance.SendMessage(message);
}

接收消息(这是在测试页面中编写的 JavaScript 函数。它们在 ASPX 页面上呈现消息的内容。在这里你应该实现你的逻辑):

// This method will persist a http request and wait for messages.
        function waitEvent() {

            CSASPNETReverseAJAX.Dispatcher.WaitMessage("<%= Session["userName"] %>", 
            function (result) {

                displayMessage(result);

                // Keep looping.
                setTimeout(waitEvent, 0);
            }, function () {

                // Keep looping.
                setTimeout(waitEvent, 0);
            });
        }

        // Append a message content to the result panel.
        function displayMessage(message) {
            var panel = document.getElementById("<%= lbMessages.ClientID %>");

            panel.innerHTML += currentTime() + ": " + message + "<br />";
        }

        // Return a current time string.
        function currentTime() {
            var currentDate = new Date();
            return currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
        }
于 2012-09-03T11:18:35.643 回答
0

asp:UpdateProgress 是来自 ASP .NET 的 AJAX 扩展,它仅与 asp:UpdatePanel 相关联工作,并且在来自此面板的事件上触发。它有一个名为 AssociatedUpdatePanelID 的属性,您必须在其中写入更新面板的 id。希望这可以帮助。

于 2012-09-03T11:23:39.440 回答