I have a button on an aspx page that is supposed to send an email… my code appears to be setup correctly but when the button is clicked, nothing happens. Here is the code. It is intended to display an alert then redirect to the success page but nothing happens…
There is no onclick, but according to this, there shouldn’t be: http://www.velocityreviews.com/forums/t367616-using-handles-vb-vs-c.html Also the guide I got this code from doesn’t use onclick either, although it’s outdated.
The markup is:
<%@ Page Title="Sign Up" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="signup.aspx.cs" Inherits="WebApplication2._Default" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Email Service
</h2>
Re-enter your email address to confirm signup for updates.
<br />
<table border="0">
<tr>
<td><b>Email:</b></td>
<td><asp:TextBox runat="server" ID="UsersEmail" Columns="30"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button runat="server" ID="SendEmail" Text="Sign Up"/>
</td>
</tr>
</table>
The code-behind is:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SendEmail_Click(object sender, EventArgs e) //Handles SendEmail.Click
{
try
{
//!!! UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
string ToAddress = "info@removed.org";
//(1) Create the MailMessage instance
MailMessage mm = new MailMessage(UsersEmail.Text, ToAddress);
//(2) Assign the MailMessage's properties
mm.Subject = UsersEmail.Text + " would like to sign up for updates";
mm.Body = "Hello, I would like to sign up for updates. My email is " + UsersEmail.Text + ". Thank you.";
mm.IsBodyHtml = false;
//(3) Create the SmtpClient object
SmtpClient smtp = new SmtpClient("[removed]");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//(4) Send the MailMessage (will use the Web.config settings)
smtp.Send(mm);
}
catch (Exception ex)
{
string strError = "<Script language=javascript>alert('Error');</Script>";
Response.Write(strError);
Response.Redirect("Success.aspx");
}
string strMsg = "<Script language=javascript>alert('Emailsent');</Script>";
Response.Write(strMsg);
Response.Redirect("Success.aspx");
//System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""Hello this is an Alert"")</SCRIPT>");
}
}
}
I really doubt onclick is the solution anyway, because that not process any code..
Any ideas? Thanks.
Add the handler to the event
Next:
If you do this:
You won’t know anything about the error, for the first reason because a Redirect is made so the javascript code won’t get the chance to run and the next reason is that catching the exception like this, doing nothing with it, you’ll loose the instructions how to fix it.
You don’t need to set the handler in markup, if you think it bloats it. You can do it for sure in code