skip to Main Content

My problem is that My asp.net Button’s onclick behaviour does not work because on code behind btnSendMail_Click show’s 0 reference. I added my code and code behind and my error’s message. Normally I have used this method several times but this time I couldn’t do it.

Some of my html code

<div class="col-lg-8" runat="server">
                <div class="contact-form section-top-gap-100" data-aos="fade-up"  data-aos-delay="200">
                    <asp:Label Text="<%$Resources:Lang, ContactUs %>" runat="server" Font-Size="Large" Font-Bold="true"></asp:Label>
                    <div id="contact-form">
                        <div class="row">
                            <div class="col-lg-6">
                                <div class="default-form-box mb-20">
                                   <asp:Label Text="<%$Resources:Lang, Name %>" runat="server" Font-Bold="true"></asp:Label>
                                    <input name="name" type="text" id="contactName" runat="server" placeholder="<%$Resources:Lang, NamePlaceHolder %>" >
                                    
                                </div>
                            </div>
                            <div class="col-lg-6">
                                <div class="default-form-box mb-20">
                                   <asp:Label Text="<%$Resources:Lang, mail %>" runat="server" Font-Bold="true"></asp:Label>
                                    <input name="email" type="email" id="contactEmail" runat="server" placeholder="<%$Resources:Lang, mailPlaceHolder %>" >
                                </div>
                            </div>
                            <div class="col-lg-12">
                                <div class="default-form-box mb-20">
                                   <asp:Label Text="<%$Resources:Lang, subject %>" runat="server" Font-Bold="true"></asp:Label>
                                    <input name="subject" type="text" id="contactSubject" runat="server" placeholder="<%$Resources:Lang, SubjectPlaceHolder %>">
                                </div>
                            </div>
                            <div class="col-lg-12">
                                <div class="default-form-box mb-20">
                                   <asp:Label Text="<%$Resources:Lang, message %>" runat="server" Font-Bold="true"></asp:Label>
                                    <textarea name="message" id="contactMessage" cols="30" rows="10" runat="server" placeholder="<%$Resources:Lang, MessagePlaceHolder %>" ></textarea>
                                </div>
                            </div>
                            <div class="col-lg-12">
                                <asp:Button runat="server" CssClass="contact-submit-btn" Text="<%$Resources:Lang, sendMessage %>" ID="btnSendMail" OnClick="btnSendMail_Click" UseSubmitBehavior="true"/>
                            </div>
                            <p class="form-messege"></p>
                            
                        </div>
                    </div>
                </div>
            </div>

My code behind

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AsjSpray.Web.Pages
{
    public partial class Iletisim : BasePage
    {
        #region Properties
        public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "AsjSpray " + Resources.Lang.ContactUs;
            log.Info("İletişim Sayfasına girildi");
        }

        private static string GetIp()
        {
            string VisitorsIPAddress = string.Empty;
            try
            {
                if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                {
                    VisitorsIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
                else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
                {
                    VisitorsIPAddress = HttpContext.Current.Request.UserHostAddress;
                }
            }
            catch (Exception ex)
            {
                log.Error("Hata [Iletisim.GetIp()]" + ex.Message);
            }
            return VisitorsIPAddress;
        }

        bool IsValidEmail(string email)
        {
            try
            {
                var addr = new System.Net.Mail.MailAddress(email);
                return addr.Address == email;
            }
            catch
            {
                return false;
            }
        }

        protected void btnSendMail_Click(object sender, EventArgs e)
        {
            var senderMessage = contactMessage.Value;
            var senderName = contactName.Value;
            var senderMail = contactEmail.Value;
            var subject = contactSubject.Value;
            var ipAddress = GetIp();


            SmtpClient smtpClient = new SmtpClient();

            NetworkCredential basicCredential = new NetworkCredential("[email protected]", "pass");

            MailMessage message = new MailMessage();

            MailAddress fromAddress = new MailAddress("[email protected]");

            smtpClient.Host = "mail.asjspray.com";

            smtpClient.UseDefaultCredentials = false;

            smtpClient.Credentials = basicCredential;

            smtpClient.Port = 587;

            smtpClient.EnableSsl = true;

            message.From = fromAddress;

            message.Subject = subject;

            //Set IsBodyHtml to true means you can send HTML email.

            message.IsBodyHtml = true;

            message.Body = "Gönderici adı: " + senderName + "<br/> Gönderici maili: " + senderMail + "<br/> Gönderici mesajı: " + senderMessage + "<br/> Gönderici ip adresi: " + ipAddress; ;

            message.To.Add("[email protected]");

            message.Bcc.Add("");


            try

            {
                ServicePointManager.ServerCertificateValidationCallback = delegate (object s,
                    X509Certificate certificate,
                    X509Chain chain,
                    SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };
                if (senderMessage != null && senderName != null &&
                    senderMail != null)
                {
                    smtpClient.Send(message);
                    ScriptManager.RegisterStartupScript(this, GetType(), "CallFunction", "successful();", true);
                    log.Info("Mail gönderme başarılı mail adresi: " + senderMail);
                }
                else if (IsValidEmail(senderMail) == false)
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "CallFunction", "emptyMail();", true);
                    log.Warn("Mail gönderilmedi mail geçersiz " + senderMail);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "CallFunction", "hata();", true);
                    log.Error("Mail gönderilmedi");
                }


            }

            catch (Exception ex)

            {
                log.Error("Hata, Mail gönderilemedi " + ex.Message);
                Response.Write(ex.Message);
                Page.Response.Write("<script>console.log('" + ex.Message + "');</script>");

            }
        }
    }
    }

My Error Image

Error Image

As you can see button_click’s reference is 0.

2

Answers


  1. Chosen as BEST ANSWER

    I solved the problem. It is because I have "required" property in MasterPage search bar. It prevented click to button.


  2. You could double check the click event.

    What most don’t realize? Well, if you nest a button in a gridview, repeater, etc?

    Well, now you can’t just click on the button and jump right to the event stub (and it automatic – always nice).

    but, you CAN get VS to generate the event stub for you. (erase your existing onclick in the button markup, and try this.

    Type in onclick=

    When you hit "=", then intel-sense should pop up this:

    enter image description here

    So, click on create new event. It LOOKS like nothing occured, but if you now flip to code behind, then you see the code stub.

    Give above a try – you should not even need that UseSubmit behavour.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search