2009-05-22 28 views
1

我想弄清楚,但我沒有太多的運氣。表單提交(電子郵件)與ASP.net和jQuery不工作

基本上,我有一個使用jQuery表單嚮導插件構建的嚮導樣式HTML表單,其中包含jQuery.Form,jQuery.Validation和jQuery.History插件。我試圖使用ASP.net Web窗體來提交電子郵件,但我似乎無法使它工作。

這裏是我的標記和代碼示例:

<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("[email protected]"); 
     mail.To.Add("[email protected]"); 

     //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); 
    } 
} 

我不知道我需要在這裏做。我對jQuery仍然很陌生,對於ASP.net來說也很新穎,所以我確信我只是錯過了一些顯而易見的東西,但我沒有嘗試過任何東西。所以如果有人能指出我在這裏正確的方向,這將非常感激。

+0

示例代碼不包括任何jQuery的,所以我很困惑,是什麼問題。 jQuery沒有運行?按鈕點擊服務器代碼沒有運行? – ern 2009-05-22 18:45:09

回答

0

如果你正在使用jquery表單提交,請刪除onclick並給表單一個id。 該ID會進入您網頁上的JavaScript代碼以收集您的帖子。

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

在你的頁面加載只需添加

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; 

}