简体   繁体   English

使用ASP.net和jQuery的表单提交(电子邮件)不起作用

[英]Form submit (email) with ASP.net and jQuery not working

I'm trying to figure this out, but I'm not having much luck. 我正在尝试解决这个问题,但是运气不佳。

Basically, I have a wizard style HTML form that's built using the jQuery form wizard plugin, which includes the jQuery.Form, jQuery.Validation and jQuery.History plugins. 基本上,我有一个使用jQuery表单向导插件构建的向导样式HTML表单,其中包括jQuery.Form,jQuery.Validation和jQuery.History插件。 I'm trying to use ASP.net Web Forms to submit an email, but I can't seem to make it work at all. 我正在尝试使用ASP.net Web表单提交电子邮件,但似乎根本无法使它工作。

here's an example of my markup and code: 这是我的标记和代码的示例:

<form id="form1" runat="server">
    <div class="step" id="step01">
        <input type="text" id="text1" name="text1" runat="server" /><br />
        <input type="text" id="text2" name="text2" runat="server" />
    </div>
    <div class="step" id="step02">
        <input type="text" id="text3" name="text3" runat="server" /><br />
        <input type="text" id="text4" name="text4" runat="server" />
    </div>
    <input type="reset" id="reset" value="Reset" />
    <asp:Button runat="server" id="submit" Text="Submit" OnClick="button_Click" />
</form>

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

public partial class email_test02 : System.Web.UI.Page
{
    protected void button_Click(object sender, EventArgs e)
    {
        //create the mail message
        MailMessage mail = new MailMessage();

        //set the addresses
        mail.From = new MailAddress("test@test.com");
        mail.To.Add("test2@test.com");

        //set the content
        mail.Subject = "Test";
        mail.Body =
            text1.Value + text2.Value + text3.Value + text4.Value;

        //send the message
        SmtpClient smtp = new SmtpClient("Localhost");
        smtp.Send(mail);
    }
}

I'm not sure what I need to do from here. 我不确定从这里开始需要做什么。 I'm still pretty new to jQuery and very new to ASP.net, so I'm sure I'm just missing something obvious, but I've had no luck with anything I've tried. 我对jQuery还是个新手,对ASP.net还是个新手,所以我确定我只是缺少一些明显的东西,但是我所做的任何事情都没有运气。 So if someone could point me in the right direction here it would be much appreciated. 因此,如果有人可以在这里指出正确的方向,将不胜感激。

If you are using jquery form for the submittion remove the onclick and give the form an id. 如果您使用jquery表单进行提交,请删除onclick并为表单提供一个ID。 The ID goes into your javascript code on the page to collect your post. 该ID进入页面上您的javascript代码中以收集您的帖子。

<script type="text/javascript"> 
     $(document).ready(function() { 
        $('#form1').ajaxForm(function() { 
            location.href=("submitted.aspx");
        }); 
    }); 
</script> 

in your page load simply add 在页面加载中只需添加

if(isPostback){
    string txt1 = Request["text1"];

    // you will want to clean this string, (encode for storage) 
    // and maybe also check for isnull or empty first i.e

    var username = !string.IsNullorEmpty(Request["text1"]) ? Request["text1"] : null;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM