C# 如何使用AjaxToolKit确认按钮扩展器?
在我的ASP.NET基于web的应用程序中,我有一个普通的ASP.NET按钮,其背后有一个发送电子邮件的方法,如下所示:C# 如何使用AjaxToolKit确认按钮扩展器?,c#,asp.net,C#,Asp.net,在我的ASP.NET基于web的应用程序中,我有一个普通的ASP.NET按钮,其背后有一个发送电子邮件的方法,如下所示: <asp:Button ID="btnSendReminders" runat="server" Text="Send Reminders" OnClick="btnSendReminders_Click" /> <ajaxToolkit:ConfirmButtonExtender ID="ConfirmButtonExtender
<asp:Button ID="btnSendReminders" runat="server" Text="Send Reminders" OnClick="btnSendReminders_Click" />
<ajaxToolkit:ConfirmButtonExtender ID="ConfirmButtonExtender1" runat="server" TargetControlID="btnSendReminders"
ConfirmText="Are you sure you want to send reminders to these employees?"
OnClientCancel="CancelClick">
</ajaxToolkit:ConfirmButtonExtender>
ASP.NET代码:
<asp:Button ID="btnSendReminders" runat="server" Text="Send Reminders" OnClick="btnSendReminders_Click" />
protected void btnSendReminders_Click(object sender, EventArgs e)
{
SendEmailTOAllUser();
}
protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
{
SmtpClient sc = new SmtpClient("Mail Server");
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("pssp@aramco.com", "PMOD Safety Services Portal (PSSP)");
// In case the mail system doesn't like no to recipients. This could be removed
//msg.To.Add("psTeesstp@DomainServer.com");
msg.Bcc.Add(toAddresses);
msg.Subject = MailSubject;
msg.Body = MessageBody;
msg.IsBodyHtml = isBodyHtml;
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
}
}
protected void SendEmailTOAllUser()
{
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspForTest;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connString))
{
var sbEmailAddresses = new System.Text.StringBuilder(2000);
string quizid = "";
// Open DB connection.
conn.Open();
string cmdText = "SELECT MAX (QuizID) As mQuizID FROM dbo.QUIZ WHERE IsSent <> 0";
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
// There is only 1 column, so just retrieve it using the ordinal position
quizid = reader["mQuizID"].ToString();
}
}
reader.Close();
}
string cmdText2 = @"SELECT e.Username, d.DivisionShortcut
FROM
employee e
join Divisions d on (e.DivisionCode = d.SapCode)
left join
(select A.QuizID, a.Username
from UserQuiz a join
(select max(QuizID) QuizID from dbo.Quiz where IsSent=1) b
on a.QuizId = b.QuizID
) c
on e.Username = c.Username
WHERE c.QuizID is null
Order By d.DivisionShortcut";
using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
var sName = reader.GetString(0);
if (!string.IsNullOrEmpty(sName))
{
if (sbEmailAddresses.Length != 0)
{
sbEmailAddresses.Append(",");
}
// Just use the ordinal position for the user name since there is only 1 column
sbEmailAddresses.Append(sName).Append("@DomainServer.com");
}
}
}
reader.Close();
// Add the parameter to the command
var oParameter = cmd.Parameters.Add("@QuizID", SqlDbType.Int);
var sEMailAddresses = sbEmailAddresses.ToString();
string link = "<a href='http://StartQuiz.aspx?testid=" + quizid + "'> Click here to participate </a>";
string body = @"<font color='red' size='18pt'><b> ***REMINDER*** </b></font> <br /><br />
Good day, <br /><br />
<b>This is just a gentle reminder asking you to participate in the last new short safety quiz </b>"
+ link +
@"<br /><br />
Also, give yourself a chance to gain more safety culture by reading the PMOD Newsletter.
<br /><br /><br />
PLEASE <span class='highlight'>IGNORE</span> THIS MESSAGE IF YOU HAVE ALREADY PARTICIPATED IN THIS QUIZ.
<br /> <br /><br /> <br />
This email was generated using the <a href='http://pmv/pssp/Default.aspx'>Safety Portal (PSSP) </a>.
Please do not reply to this email.";
int sendCount = 0;
List<string> addressList = new List<string>(sEMailAddresses.Split(','));
StringBuilder addressesToSend = new StringBuilder();
for (int userIndex = 0; userIndex < addressList.Count; userIndex++)
{
sendCount++;
if (addressesToSend.Length > 0)
addressesToSend.Append(",");
addressesToSend.Append(addressList[userIndex]);
if (sendCount == 10 || userIndex == addressList.Count - 1)
{
SendEmail(addressesToSend.ToString(), "", "REMINDER: Notification of New Weekly Safety Quiz", body, true);
addressesToSend.Clear();
sendCount = 0;
}
}
// Update the parameter for the current quiz
oParameter.Value = quizid;
// And execute the command
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
”;
字符串正文=@“***提醒***
你好,
这只是一个温和的提醒,请您参加上一次新的简短安全测验”
+链接+
@“
此外,通过阅读PMOD时事通讯,给自己一个获得更多安全文化的机会。
如果您已经参加了此测验,请忽略此消息。
此电子邮件是使用生成的。
请不要回复此电子邮件。“;
int sendCount=0;
List addressList=新列表(sEMailAddresses.Split(',');
StringBuilder addressesToSend=新建StringBuilder();
对于(int userIndex=0;userIndex0)
addressesToSend.Append(“,”);
addressossend.Append(addressList[userIndex]);
if(sendCount==10 | | userIndex==addressList.Count-1)
{
SendEmail(addressesToSend.ToString(),“”,“提醒:每周新安全测验通知”,正文,真);
addressesToSend.Clear();
sendCount=0;
}
}
//更新当前测验的参数
oparmeter.Value=quizid;
//然后执行命令
cmd.ExecuteNonQuery();
}
康涅狄格州关闭();
}
}
现在,我不想使用
,而是想使用:
<ajaxToolkit:ConfirmButtonExtender ID="btnSendReminders" runat="server" TargetControlID="btnSendReminders"
ConfirmText="Are you sure you want to click this?"
OnClientCancel="CancelClick" />
有一个确认消息。但我不知道为什么我会出现以下错误:
**
扩展程序控件“btnSendReminders”无法扩展“btnSendReminders”。
类型为“AjaxControlToolkit.confirButtoneXtender”的扩展程序控件
无法扩展类型为的控件
“AjaxControlToolkit.confirButtoneXtender”
我不知道为什么。那个么我怎样才能用它来代替
更新:
<asp:Button ID="btnSendReminders" runat="server" Text="Send Reminders" OnClick="btnSendReminders_Click" />
protected void btnSendReminders_Click(object sender, EventArgs e)
{
SendEmailTOAllUser();
}
protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
{
SmtpClient sc = new SmtpClient("Mail Server");
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("pssp@aramco.com", "PMOD Safety Services Portal (PSSP)");
// In case the mail system doesn't like no to recipients. This could be removed
//msg.To.Add("psTeesstp@DomainServer.com");
msg.Bcc.Add(toAddresses);
msg.Subject = MailSubject;
msg.Body = MessageBody;
msg.IsBodyHtml = isBodyHtml;
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
}
}
protected void SendEmailTOAllUser()
{
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspForTest;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connString))
{
var sbEmailAddresses = new System.Text.StringBuilder(2000);
string quizid = "";
// Open DB connection.
conn.Open();
string cmdText = "SELECT MAX (QuizID) As mQuizID FROM dbo.QUIZ WHERE IsSent <> 0";
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
// There is only 1 column, so just retrieve it using the ordinal position
quizid = reader["mQuizID"].ToString();
}
}
reader.Close();
}
string cmdText2 = @"SELECT e.Username, d.DivisionShortcut
FROM
employee e
join Divisions d on (e.DivisionCode = d.SapCode)
left join
(select A.QuizID, a.Username
from UserQuiz a join
(select max(QuizID) QuizID from dbo.Quiz where IsSent=1) b
on a.QuizId = b.QuizID
) c
on e.Username = c.Username
WHERE c.QuizID is null
Order By d.DivisionShortcut";
using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
if (reader != null)
{
while (reader.Read())
{
var sName = reader.GetString(0);
if (!string.IsNullOrEmpty(sName))
{
if (sbEmailAddresses.Length != 0)
{
sbEmailAddresses.Append(",");
}
// Just use the ordinal position for the user name since there is only 1 column
sbEmailAddresses.Append(sName).Append("@DomainServer.com");
}
}
}
reader.Close();
// Add the parameter to the command
var oParameter = cmd.Parameters.Add("@QuizID", SqlDbType.Int);
var sEMailAddresses = sbEmailAddresses.ToString();
string link = "<a href='http://StartQuiz.aspx?testid=" + quizid + "'> Click here to participate </a>";
string body = @"<font color='red' size='18pt'><b> ***REMINDER*** </b></font> <br /><br />
Good day, <br /><br />
<b>This is just a gentle reminder asking you to participate in the last new short safety quiz </b>"
+ link +
@"<br /><br />
Also, give yourself a chance to gain more safety culture by reading the PMOD Newsletter.
<br /><br /><br />
PLEASE <span class='highlight'>IGNORE</span> THIS MESSAGE IF YOU HAVE ALREADY PARTICIPATED IN THIS QUIZ.
<br /> <br /><br /> <br />
This email was generated using the <a href='http://pmv/pssp/Default.aspx'>Safety Portal (PSSP) </a>.
Please do not reply to this email.";
int sendCount = 0;
List<string> addressList = new List<string>(sEMailAddresses.Split(','));
StringBuilder addressesToSend = new StringBuilder();
for (int userIndex = 0; userIndex < addressList.Count; userIndex++)
{
sendCount++;
if (addressesToSend.Length > 0)
addressesToSend.Append(",");
addressesToSend.Append(addressList[userIndex]);
if (sendCount == 10 || userIndex == addressList.Count - 1)
{
SendEmail(addressesToSend.ToString(), "", "REMINDER: Notification of New Weekly Safety Quiz", body, true);
addressesToSend.Clear();
sendCount = 0;
}
}
// Update the parameter for the current quiz
oParameter.Value = quizid;
// And execute the command
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
我刚刚想出了如何使用它。我应该按如下方式使用它:
<asp:Button ID="btnSendReminders" runat="server" Text="Send Reminders" OnClick="btnSendReminders_Click" />
<ajaxToolkit:ConfirmButtonExtender ID="ConfirmButtonExtender1" runat="server" TargetControlID="btnSendReminders"
ConfirmText="Are you sure you want to send reminders to these employees?"
OnClientCancel="CancelClick">
</ajaxToolkit:ConfirmButtonExtender>
但现在的问题是,当出现确认消息并单击“取消”按钮时,页面仍将刷新并发送提醒。如果单击“取消”按钮,如何防止出现这种情况?您可以使用客户端返回确认来执行相同的操作
<asp:Button ID="btnSendReminders" OnClientClick="javascript:return confirm('Are you sure you want to proceed?')" runat="server" Text="Send Reminders" OnClick="btnSendReminders_Click" />
您可以使用客户端返回确认来执行相同的操作
<asp:Button ID="btnSendReminders" OnClientClick="javascript:return confirm('Are you sure you want to proceed?')" runat="server" Text="Send Reminders" OnClick="btnSendReminders_Click" />
使用以下代码确认按钮ajax extender获取确认框:
<asp:Button ID="btnSendReminders" runat="server" Text="Send Reminders" OnClick="btnSendReminders_Click" />
<ajaxToolkit:ConfirmButtonExtender ID="cbe" runat="server"
TargetControlID="btnSendReminders"
ConfirmText="Are you sure you want to Send Reminders?"
/>
使用以下代码确认按钮ajax extender获取确认框:
<asp:Button ID="btnSendReminders" runat="server" Text="Send Reminders" OnClick="btnSendReminders_Click" />
<ajaxToolkit:ConfirmButtonExtender ID="cbe" runat="server"
TargetControlID="btnSendReminders"
ConfirmText="Are you sure you want to Send Reminders?"
/>
谢谢你的帮助。我真的很感激。谢谢你的帮助。我真的很感激。谢谢你的帮助。我真的很感激。谢谢你的帮助。我真的很感激。