Ghazanfar Khan
Ghazanfar Khan

Reputation: 3718

OnClick event not firing on any browser

I have a simple form on my index.aspx page which sends email on button click in code behind file

<form runat="server" action="index.aspx">

    <label>Name</label>
    <input type="text" runat="server" class="form-control" placeholder="Name" id="name" required data- validation-required-message="Please enter your name.">
    <label>Email Address</label>
    <input type="email" runat="server" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
    <label>Phone Number</label>
    <input type="tel" runat="server" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
    <label>Message</label>
    <textarea rows="5" runat="server" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>

    <asp:Button runat="server" Text="Send" OnClick="Button1_Click" CssClass="btn btn-success btn-lg"></asp:Button>

</form>

Button1_Click event not firing on any browser.On my code behind file I have following code

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        // Gmail Address from where you send the mail
        var fromAddress = Request.Form["email"];
        // any address where the email will be sending
        var toAddress = "[email protected]";
        //Password of your gmail address
        const string fromPassword = "abc";
        // Passing the values and make a email formate to display
        string subject = "Feedback of COMIC MAKER";
        string body = "From: " + Request.Form["name"] + "\n";
        body += "Subject: " + Request.Form["message"] + "\n";
        body += "Question: \n" + Request.Form["phone"] + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);
    }
    catch { }
}

Nothing is happening , I have put breakpoints but nothing is happening.Need help?

Upvotes: 0

Views: 181

Answers (1)

Birgit Martinelle
Birgit Martinelle

Reputation: 1889

The solution is to add UseSubmitBehavior="False" So you end up with this for your button:

 <asp:Button runat="server" Text="Send" OnClick="Button1_Click" UseSubmitBehavior="False" CssClass="btn btn-success btn-lg"></asp:Button>

The reason for this is that the default UseSubmitBehavor is set to True, which means that the Button control uses the client browser's submit mechanism. What you want however, is to use the asp postback mechanism (asp generates a block of scripts, part of it is the _doPostBack function:

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

Setting your UseSubmitBehavior="False" will trigger this function call and the postback to your server.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior(v=vs.110).aspx

and here:

http://dotnetprof.blogspot.com/2012/08/dopostback-function-in-javascript.html

Upvotes: 1

Related Questions