Posting a method by clicking a button type = button

0

I'm making an application where when I click a button I have to send an email. I have my page a input type=Text and a button type="button" that with css is thus -> .
I need that when I load that arrow, an email is sent as it is in the code, and the email body is also sent what is in the input text. If the input text is empty, it should give error and do nothing.

I click the button but it does nothing. What can go wrong?

Code I have for now:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Cegos Analytics | Lockscreen</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.5 -->
  <link rel="stylesheet" href="../../bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../../dist/css/AdminLTE.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script><scriptsrc="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>
<body class="hold-transition lockscreen">
<!-- Automatic element centering -->
<div class="lockscreen-wrapper">
  <div class="lockscreen-logo">
   <img src="/Images/^082594892CD6A1E2D2CF4B7361463D84FEF2B4C6FEBD2707B5^pimgpsh_fullsize_distr.png" alt="Cegos Logo">
  </div>
    <br />
  <!-- User name -->
  <div class="lockscreen-name"></div>

  <!-- START LOCK SCREEN ITEM -->
  <div class="lockscreen-item">
    <!-- lockscreen image -->
    <div class="lockscreen-image">
      <img src="/Images/locked.jpg" alt="Locked Screen">
    </div>
    <!-- /.lockscreen-image -->

    <!-- START LOCK SCREEN ITEM -->
  <div class="lockscreen-item">
    <!-- lockscreen image -->
    <div class="lockscreen-image">
      <img src="/Images/locked.jpg" alt="Locked Screen">
    </div>
    <!-- /.lockscreen-image -->
</div>

    <!-- lockscreen credentials (contains the form) -->
    <form class="lockscreen-credentials">
      <div class="input-group">
        <input type="Text" class="form-control" placeholder="Username">


    <%@ Import Namespace="System.Net.Mail" %> 
    <%@ Import Namespace="System.Net" %>
    <%@ Import Namespace="System.Net" %>
    <%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="System.Text" %>

  <script runat="server">

      public void SendEmail()
      {


          try
          {

              SmtpClient cliente = new SmtpClient();
              cliente.Host = "smtp.sapo.pt";
              cliente.EnableSsl = true;
              cliente.Credentials = new NetworkCredential("[email protected]", "cegoc123456");

              MailMessage mensagem = new MailMessage();
              mensagem.Sender = new MailAddress("[email protected]");
              mensagem.From = new MailAddress("[email protected]");
              mensagem.To.Add(new MailAddress("[email protected]"));
              mensagem.Subject = "Pedido de suporte Plataforma Cegos Analytics";
              mensagem.Body = "Recebeu um pedido de suporte da  Plataforma Cegos Analytics para recuperação de password referente ao utilizador:";
              mensagem.IsBodyHtml = false;
              mensagem.Priority = MailPriority.High;

              cliente.Send(mensagem);
          }
          catch (Exception ex)
          {
              Response.Write("******EMAIL FAILED. PLEASE CONTACT YOUR ADMINISTRATOR**** See log file in C:\Logs");
              CreateLogFiles(ex.Message);
              ErrorLog("C:\Logs\log.txt", ex.Message);
              Response.Write(ex.Message);

          }


      }

</script>  


        <div class="input-group-btn">
          <button type="button" class="btn" onclick="SendEmail()" runat="server"><i class="fa fa-arrow-right text-muted"></i></button>
         </div>
      </div>
    </form>
    <!-- /.lockscreen credentials -->

  </div>
  <!-- /.lockscreen-item -->
  <div class="help-block text-center">
    Enter your username to retrieve your session
  </div>
  <div class="text-center">
    <a href="../Default.aspx">Or sign in as a different user</a>

What I essentially have is a page recoverpassword.aspx where I have this:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Cegos Analytics | Lockscreen</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.5 -->
  <link rel="stylesheet" href="../../bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../../dist/css/AdminLTE.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script><scriptsrc="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>
<body class="hold-transition lockscreen">
<!-- Automatic element centering -->
<div class="lockscreen-wrapper">
  <div class="lockscreen-logo">
   <img src="/Images/^082594892CD6A1E2D2CF4B7361463D84FEF2B4C6FEBD2707B5^pimgpsh_fullsize_distr.png" alt="Cegos Logo">
  </div>
    <br />
  <!-- User name -->
  <div class="lockscreen-name"></div>

  <!-- START LOCK SCREEN ITEM -->
  <div class="lockscreen-item">
    <!-- lockscreen image -->
    <div class="lockscreen-image">
      <img src="/Images/locked.jpg" alt="Locked Screen">
    </div>
    <!-- /.lockscreen-image -->

    <!-- START LOCK SCREEN ITEM -->
  <div class="lockscreen-item">
    <!-- lockscreen image -->
    <div class="lockscreen-image">
      <img src="/Images/locked.jpg" alt="Locked Screen">
    </div>
    <!-- /.lockscreen-image -->
</div>

    <!-- lockscreen credentials (contains the form) -->
    <form class="lockscreen-credentials" action="RecoverPassword.aspx" method="post">
      <div class="input-group">
         <input id='user' type="Text" class="form-control" placeholder="Username">    
    <div class="input-group-btn">
              <button id='enviar' type="submit" class="btn"><i class="fa fa-arrow-right text-muted"></i></button>
         </div>

     </div>
      </div>
    </form>
    <!-- /.lockscreen credentials -->

  </div>
  <!-- /.lockscreen-item -->
  <div class="help-block text-center">
    Enter your username to retrieve your session
  </div>
  <div class="text-center">
    <a href="../Default.aspx">Or sign in as a different user</a>
  </div>
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
  <%--  <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />--%>
    <br />
  <div class="lockscreen-footer text-center">
        <p style="height: 42px" align="center">&copy; <%: DateTime.Now.ToShortDateString() %> - <strong>Cegos Elearning Reports</strong>
  </div>
</div>
<!-- /.center -->

<!-- jQuery 2.2.0 -->
<script src="../../plugins/jQuery/jQuery-2.2.0.min.js"></script>
<!-- Bootstrap 3.3.5 -->
<script src="../../bootstrap/js/bootstrap.min.js"></script>    

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script><scripttype="text/javascript">

    $("#enviar").click(function () {
               @SendEmail();//Insira aqui o código para chamar o sender email.
    });
</script>



            </form>

            </body>

</html>

and I have in the page itself I have a class recoverpasssword and I defined the method sendemail () that has the code to send email. When I click the button on the page to send email it does not post nor send mail. Can you help me please? Sorry for my mistakes.

    
asked by anonymous 12.04.2016 / 18:49

1 answer

0

The problem is that you are trying to call an ASP function in the OnClick() event of JavaScript. One does not see the other as ASP runs on the server and JavaScript executes on the user's browser.

Suggestion

Modify the element type button <button type="button" ..., defining with type submit <button type="submit" ...

This topic might be of interest to learn more: Element button submitting form. How to disable?

Also define the action attribute of element <form>

<form class="lockscreen-credentials" action="pagina.aspx" method="post">

The page.asp file is where you should have the script you posted

 public void SendEmail()
  {


      try
      {

          SmtpClient cliente = new SmtpClient();
          cliente...

Runat = server

If you want to use runat="server" attribute:

<button type="button" class="btn" onclick="SendEmail()" runat="server">

Switch By:

<asp:Button id="SendEmail" runat="server" UseSubmitBehavior="false" EnableViewState="false" class="btn" onclick="SendEmail">
    
12.04.2016 / 18:55