jQuery Dialog用于发送电子邮件

时间:2013-12-06 16:48:11

标签: c# jquery html asp.net email

有点奇怪的问题。我正在尝试从jQuery对话框发送电子邮件,但似乎我坚持将输入值发送到hiddenfields以便能够发送。

这是我使用的jQuery ......

的javascript

function sendEmail() {
   $("#email").dialog({
       modal: true,
       width: 550          
    });
  }

这是使用的div。

HTML

<div class="popUpStyle" title="Send Email" id="email" style="display: none">
        <asp:Label ID="lblTo" runat="server" Text="To: "></asp:Label><asp:Label runat="server" ID="lblSendTo" Text=""></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="lblFrom" runat="server" Text="From: "></asp:Label><asp:Label runat="server" ID="lblSendFrom" Text="Training.Registration@JeffWyler.com"></asp:Label>
        <br />
        <asp:Label ID="lblSubject" runat="server" Text="Subject: "></asp:Label><input id="tbSubject" type="text"/>
        <br />
        <asp:Label ID="lblBody" runat="server" Text="Message:"></asp:Label>
        <br />
        <input ID="tbMessage" runat="server" />
        <br />
        <asp:Button ID="btnSend" runat="server" Text="Send" Width="50px" Font-Size="smaller" UseSubmitBehavior="false" OnClick="btnSend_Click" />
    </div>

C#代码背后

//Button Click Event
MailMessage msg = new MailMessage();
HttpContext ctx = HttpContext.Current;
msg.To.Add(new MailAddress(lblSendTo.Text));
msg.From = new MailAddress("Training.Registration@example.com");
msg.Subject = hfSubject.Value.ToString();
msg.Priority = MailPriority.High;
msg.Body = tbMessage.Text;

//CONFIGURE SMTP OBJECT
SmtpClient smtp = new SmtpClient("mail.example.com");

//SEND EMAIL
smtp.Send(msg);

问题在于我不确定如何将输入中的值写入隐藏字段或服务器端上的其他控件以便能够发送电子邮件。我知道点击发送按钮后会删除这些值。这最终会发送电子邮件(在另一个按钮上单击,我将lblSendTo显示为收件人电子邮件地址)但没有需要输入的字段,例如主题和邮件正文。任何帮助是极大的赞赏!

1 个答案:

答案 0 :(得分:1)

您只需要使用asp文本框,其中的文本应该在服务器端可用

根据您的评论:请注意更新

显而易见的问题是您的对话框没有附加到表单中,因此在回发期间不会在请求中发送文本框中的值。

    <a href="javascript:void(0)" onclick="sendEmail()">Send email</a> // Assuming this is the anchor used to open the dailog, you can change it your way

    <asp:Label ID="lblSubject" runat="server" Text="Subject: "></asp:Label>
    <asp:TextBox ID="tbSubject" runat="server" />
    <br />
    <asp:Label ID="lblBody" runat="server" Text="Message:"></asp:Label>
    <br />
    <asp:TextBox ID="tbMessage" runat="server" />
    <asp:Button ID="btnSend" runat="server" Text="Send" Width="50px" Font-Size="smaller" UseSubmitBehavior="false" OnClick="btnSend_Click"  />

在您的代码中

    msg.To.Add(new MailAddress(lblSendTo.Text));
    msg.From = new MailAddress("Training.Registration@example.com");
    msg.Subject = tbSubject.Text;
    msg.Priority = MailPriority.High;
    msg.Body = tbMessage.Text;

现在是剧本

   jQuery(document).ready(function () { // create the dialog right when the document is ready
     jQuery("#email").dialog({
       autoOpen: false,
       modal: true,
       width: 550          
     });
     jQuery("#email").parent().appendTo(jQuery("form:first")); // This will append the dialog to the form ensuring values are sent during postback.
   });

   function sendEmail() {
    jQuery('#email').dialog('open'); // Open the dialog now
    return false;
   }