【问题标题】:Call Server Side Function On HTML Button Click在 HTML 按钮单击上调用服务器端函数
【发布时间】:2020-03-01 10:36:08
【问题描述】:

当我点击它时我有一个按钮...首先我的 DIV 被执行/加载,这对我来说工作正常。

现在并行我想向我的帐户发送一封电子邮件,我如何调用我的服务器端功能。意味着当管理员单击“邮件帐户详细信息”时,我想弹出 DIV 并调用我的服务器端函数名称“SendAnEmail”。

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#patPortalCredDiv">Mail Account Detail</button>

这是我的 Div 代码。

 <div id="patPortalCredDiv" class="modal fade" role="dialog">
 <div class="modal-dialog">
 <!-- Modal content-->
 <div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title" style="color: black;">Mail Credential</h4>
  </div>
  <div class="modal-body">
    <div class="container-fluid">
      <div class="row">
        <div class="form-group">
          <h4 for="UserName">Note:-
           <asp:Label type="text" runat="server" Text="" ID="txtMail"></asp:Label>
          </h4>

        </div>
      </div>
 </div>
 </div>
 <div class="modal-footer">
 <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

这是我也想调用的服务器端函数。

protected void SendAndEmail(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Server Side Code Also executed.');", true);

        }

【问题讨论】:

  • 这些不是 asp.net 控件,而是普通的 Html。 SendAndEmail 被执行了吗?
  • @derloopkat 不,先生,它在按钮单击期间不执行。
  • 您使用的是 MVC 还是 Web 表单?
  • @MdFaridUddinKiron 先生,我正在使用 Web 表单,当我点击按钮时,我想显示 div 并想执行“SendAndEmail”服务器端功能。

标签: c# jquery asp.net bootstrap-modal


【解决方案1】:

本例中的问题是 data-toggledata-target 属性阻止页面回发。

在您的 aspx 页面中替换您的按钮:

<asp:Button Text="Mail Account Detail" OnClick="SendAndEmail" 
    CssClass="btn btn-primary" type="button" runat="server" />

在你的C#代码后面

protected void SendAndEmail(object sender, EventArgs e)
{
    ClientScript.RegisterStartupScript(this.GetType(), "alert", 
        "$('#patPortalCredDiv').modal('show');", true);

}

这将使用 jQuery 打开引导模式

【讨论】:

    【解决方案2】:

    这就是我所做的。 我正在使用 HTML 和 Java 作为后端来发送邮件。

    <body>
        <h3> Please Enter your Email Address </h3>
        <form action="Sendmail" method="post">
        <input type="text" class="form-control" placeholder="staff_email"  email="staff_email">
        <input type="submit" value="Submit">
        </form>
     </body>
    

    在后端,我使用的是 Gmail 服务。

    public class EmailUtility{
    public static void sendEmail(String host, String port,
            final String senderEmail, String senderName, final String password,
            String recipientEmail, String subject, String message) throws AddressException,
            MessagingException, UnsupportedEncodingException {
    
        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
    
        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(senderEmail, password);
            }
        };
    
        Session session = Session.getInstance(properties, auth);
    
        //creates a new e-mail message
        Message msg = new MimeMessage(session);
    
        msg.setFrom(new InternetAddress(senderEmail));
        InternetAddress[] toAddresses = { new InternetAddress(recipientEmail) };
        msg.setRecipients(Message.RecipientType.TO, toAddresses);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setText(message);
    
        //sends the e-mail
        Transport.send(msg);
        }
    }
    

    我正在使用 Tomcat 服务器。请在 Tomcat 服务器的 web.xml 中添加以下行。

    <context-param>
    <param-name>email</param-name>
    <param-value>abc@gmail.com</param-value>
    </context-param>
    <context-param>
    <param-name>name</param-name>
    <param-value><Your Name></param-value>
    </context-param>
    <context-param>
    <param-name>pass</param-name>
    <param-value> Your Password Comes Here </param-value>
    </context-param>
    

    【讨论】:

    • OP 不是在询问如何发送电子邮件,而是在调用服务器端函数后打开 Bootstrap 模式对话框。另外,这个问题是 C# 而不是 Java。
    • @derloopkat 谢谢先生,它工作正常,是的,我正在使用 C# (ASP.NET)。
    猜你喜欢
    • 2015-09-03
    • 2021-03-27
    • 2021-10-17
    • 2017-09-21
    • 2013-12-11
    • 2020-08-29
    • 2018-12-11
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多