如何使用 ASP.NET (C#) 在服务器上显示已发送电子邮件的计数?
让我们看看我的场景:
- 我在 UpdatePanel 中有一个按钮。
- 此按钮应向我的用户(服务器端)发送一些电子邮件。
当我点击那个按钮时,我还想做一些像这样的事情:
- 显示一个低不透明度的模态 div,中间有一个标签 它(该标签的文本为“0”)。
- 开始发送邮件。
- 该标签的文本应更新以反射(reflect)每封已发送的电子邮件(这是一个计数器)(0->1->2->...)
- 在发送邮件结束时我想隐藏那个模态 div
还有我的代码:
ASPX:
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
<asp:Image ID="imgAjax" runat="server" ImageUrl="~/Images/ajax-loader.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="RadbtnSendEmails_Container">
<telerik:RadButton ID="RadbtnSendEmails" runat="server" EnableEmbeddedSkins="False"
Skin="BlackByMe" Text="Send Mails" Width="150px" OnClick="RadbtnSendEmails_Click"
Font-Bold="True" Font-Names="Tahoma" ValidationGroup="A">
</telerik:RadButton>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<div id="OffDiv">
</div>
<div id="CounterContainer">
<asp:Image ID="imgWhiteCircle" runat="server" ImageUrl="~/Images/WhiteCircle.png" />
<asp:Label ID="lblCounter" runat="server" Text="0" Font-Bold="True"
Font-Size="50px" ForeColor="#6600CC">
</asp:Label>
</div>
C
protected void RadbtnSendEmails_Click(object sender, EventArgs e) {
ScriptManager.RegisterStartupScript(this, this.GetType(), "Show_Counter",
"Show_Counter();", true);
//int Counter=0;
//Send Mail Codes in for()
//Counter++; in every send
//Change that label'text to new counter -> i do n't know how can i do that
//ScriptManager.RegisterStartupScript(this, this.GetType(), "Hide_Counter",
"Hide_Counter();", true);
}
但是这个 C# 代码有一个问题:
当我单击该按钮时,UpdateProgress 中的图像会自行显示,在此 block 结束发送电子邮件后,我们可以看到模态 div,但在流程开始时看不到!
此外,我不知道如何实现该计数器!我应该从客户端代码跳转到服务器端代码,还是从服务器端代码跳转到客户端代码。
这些电子邮件地址位于 Sql Server 2008R2 数据库中。所以我必须在服务器端运行那部分流程。
我真的很想知道如何解决这个问题,我觉得这个场景很好,所以请帮助我了解一下。
最佳答案
因此,您面临的问题是您需要在服务器端做一些工作,但又想显示该客户端。问题的第一部分是 HTTP 是无状态无连接协议(protocol),因此您必须不断向服务器询问更多信息(更新)。
所以你需要的是几个资源:
服务器知道当前发送的电子邮件数量的方法(比如您可以访问的公共(public)计数器),服务器运行以发送电子邮件的方法(这通常需要运行一个后台线程,但要小心,一个崩溃的后台线程 - 比如说来自未处理的异常 - 将崩溃整个应用程序池)和一种从客户端初始化进程的方法(你已经有了这部分)。
然后在客户端上,您需要一种方法来发起请求,一种方法来检查更新并知道您何时完成,以及管理您想要的所有其他事情的方法。
我假设您正在使用 jQuery(这是一个基于 javascript 的库,但不会取代 javascript,这似乎是您需要学习的东西)并在此处编写一些基本代码。我还假设您在页面上运行了 Microsoft ASP.NET 库(您的代码引用了 ScriptManager,所以这是一个非常安全的选择)。
让我们从 C# 开始:(这在您的页面中,我不打算写所有内容,只是其中的大部分)
//this is a class you need to write, see below
//this way other code can access this variable too
public MySpecialMailerClass mailer;
[WebMethod] //this may require a include, you're on your own for that
//for now (System.Web.Services)
public void StartEmails(){
//do some stuff here to start the email process?
//create a class that can run the db scripts uninterrupted,
//and provide it a public member that can be accessed for the current
//count, and the expected max count (set to -1 on init so you know
//that you haven't finished initting this yet)
mailer = new MySpecialMailerClass();
new Thread( mailer.Start ) { IsBackground = true }.Start( );
}
[WebMethod]
public int GetMaxCount(){
return mailer.MaxCount;
}
[WebMethod]
public int GetCurrentCount(){
return mailer.CurrentCount;
}
这实际上就是您现在需要添加的所有 C# 代码。
Javascript:
var checkingOnEmailsSentIntervals,
checkingOnMaxMessagesIntervals,
maxMessages = 0;
function SuccessHandler(){
//Note that you should do something here to handle the success each
//time or that you should write a separate "SuccessHandler"
// for each one that you want to execute _when the server call is done_
// Note that this is what I do (one success per call)
// But in this case a generic one is sometimes handy, such as here
}
function FailHandler() {
//Note that you should do something here to handle the failure each time
}
function startScript(){
PageMethods.StartEmails(StartEmailsSuccessHandler,FailHandler);
}
function StartEmailsSuccessHandler(){
//this will get triggered when the emails have started sending,
// so this is where you create the boxes you wanted earlier, etc.
$('#counter-span-identifier').show();
checkingOnEmailsSentIntervals = setInterval(CheckForMoreMessages,100);
checkingOnMaxMessagesIntervals = setInterval(CheckForMaxMessages,10);
}
function CheckForMoreMessages() {
PageMethods.GetCurrentCount(CheckForMoreMessagesSuccessHandler,FailHandler);
}
function CheckForMoreMessagesSuccessHandler(result) {
var currentCount = result;
$('#counter-span-identifier').text(currentCount + ' messages sent');
if (currentCount == maxMessages) {
//we have finished now, so we can display another message or hide our div
clearInterval(checkingOnEmailsSentIntervals);
$('#counter-span-identifier').hide();
}
}
function CheckForMaxMessages() {
PageMethods.GetMaxCount(CheckForMaxMessagesSuccessHandler,FailHandler);
}
function CheckForMaxMessagesSuccessHandler(result) {
maxMessages = parseInt(result); //parseInt not really needed here,
//shown as example of coercion
if (maxMessages > -1){
clearInterval(checkingOnMaxMessagesIntervals);
}
}
这应该非常巧妙地结束了这里的情况,因为我们在 javascript 中有两个循环 (setInterval) 来检查 MaxMessages(因此返回 -1)和 CurrentCount。我们还有代码在每个循环获得所需信息后停止。
留给您做的部分是将发送电子邮件的代码放入一个单独的类中,该类公开成员变量和一个方法(并重命名我的示例以使其符合您的要求)。
因为我认为它需要完成,这里是 MySpecialMailerClass
的 C# 开始:
public class MySpecialMailerClass {
public MySpecialMailerClass() {
MaxCount = -1;
CurrentCount = 0;
}
public int MaxCount {get;set;}
public int CurrentCount {get;set;}
public void Start(){
//do something to get the MaxCount from the database
//do something to get the messages
var messages = mailerDbClass.GetMessagesToBeSent();
MaxCount = messages.Count;
//send the messages
foreach(message in messages) {
SendMessage(message);
CurrentCount += 1;
}
}
}
关于c# - 如何在服务器上发送邮件时在客户端显示已发送邮件的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11057760/