2012-04-05 70 views
-1
<div class="row"> 
           <label for="email">E-mail Address</label> 
           <div class="col"> 
            <div class="text"><asp:TextBox ID="TextBox2" runat="server" 
              onchange="javascript:updateHiddenField();"></asp:TextBox><asp:HiddenField ID="HiddenField1" 
               runat="server" /> 
            </div> 
            <span>Find e-mail on <a href="#">My Address Book</a>/<a href="#">Contacts</a></span> 
            <span>Find e-mail on 
             <asp:HyperLink ID="HyperLink1" runat="server">Facebook Profile</asp:HyperLink></span> 
           </div> 
          </div> 
          <div class="row"> 
           <label>Invite Type</label> 
           <div class="col"> 
            <div class="inner-row"> 
             <asp:RadioButton runat="server" ID="r1" Checked="true" GroupName="r" 
              /> 
             <label for="id-1"><em>Personalized</em> - Add a personal note to your invitation. This invite type will let your friend know of your identity and will include your personal note</label> 
            </div> 
            <div class="inner-row"> 
             <asp:RadioButton runat="server" ID="r2" GroupName="r" /> 
             <label for="id-2"><em>Anonymous</em> - Sending an invite anonymously will not reveal your indentity to your friend.</label> 
            </div> 
           </div> 
          </div> 
          <div class="row"> 
           <label for="message">Personal Note</label> 
           <div class="col"> 
            <div class="textarea"> 
             <div class="textarea-holder"> 
              <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Rows="10" 
               Columns="30" onchange="javascript:updateHiddenField1();"></asp:TextBox><asp:HiddenField 
               ID="HiddenField2" runat="server" /> 
             </div> 
            </div> 
            <span class="text-note">Including a note is optional, but isn’t<br />personalized messages always nice :)</span> 
           </div> 
          </div> 
<asp:LinkButton ID="LinkButton6" runat="server" onClick="LinkButton6_Click" CssClass="btn-send"></asp:LinkButton> 

這是我的aspx代碼,我想從文本框第一傳輸值,然後hiddenfield然後代碼隱藏值不會從JavaScript傳遞給代碼隱藏asp.net?

<script type="text/javascript"> 
    function updateHiddenField() { 
     document.getElementById('HiddenField1').value = document.getElementById('TextBox2').value; 

    } 

    function updateHiddenField1() { 
     document.getElementById('HiddenField2').value = document.getElementById('TextBox1').value 
    } 
</script> 
當我這樣做 alert(document.getElementById('HiddenField1').value);

但是他們不是

值越來越顯示傳遞到代碼隱藏

public void LinkButton6_Click(object sender, EventArgs e) { 

try{ 
      System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); 
      mail.Subject = ""; 
      mail.Body = HiddenField1.Value; 
      mail.To.Add(HiddenField2.Value); 

      ////send the message 

      System.Net.Mail.SmtpClient mySmtpClient = new System.Net.Mail.SmtpClient(); 
      System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("",""); 
      //mySmtpClient.Port="25"; 
      mySmtpClient.Host = ""; 
      mySmtpClient.UseDefaultCredentials = false; 
      mySmtpClient.Credentials = myCredential; 
      mySmtpClient.Send(mail); 
     } 
     catch (Exception ex) 
     { 
      Response.Write(ex.ToString()); 
     } 

enter image description here

編輯:

  1. 我已經嘗試使用mail.Body = TextBox2.Text; mail.To.Add(TextBox1.Text);

  2. 我曾嘗試沒有在UpdatePanel

  3. 的代碼在CSS的彈出窗口,是主要的問題

建議我使用html按鈕來調用javascript函數

function btn_click() { 
     var email = document.getElementById('HiddenField1').value; 
     var body = document.getElementById('HiddenField2').value; 

    } 

如何傳遞這個值作爲我的Asp.net

請憑據幫助

+1

你爲什麼不乾脆用你的代碼隱藏文本框的值? – jrummell 2012-04-05 12:23:40

+0

我試過它沒有工作相同的錯誤,所以嘗試使用這種相同的事情也發生在這裏以及 – 2012-04-05 12:24:29

+0

這是在一個用戶控件或有任何其他可能不明顯從您的代碼示例? – jrummell 2012-04-05 12:26:52

回答

2
<script type="text/javascript"> 
function SendForm() { 

     var email = $get("TextBox2").value; 
     var message = $get("TextBox1").value; 

     PageMethods.SendForm(email, message, 
    OnSucceeded, OnFailed); 
    } 

    function OnSucceeded() { 
     // Dispaly "thank you." 
     $get("ContactFieldset").innerHTML = "<p>Thank you!</p>"; 
    } 

    function OnFailed(error) { 
     // Alert user to the error. 
     alert(error.get_message()); 
    } 
</script> 

,並在使用後面Asp.net代碼:

[WebMethod] 
public static void SendForm(string email, string message) 
{ 


    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); 
     mail.Subject = ""; 
     mail.From = new MailAddress(""); 
     mail.Body = message; 
     mail.To.Add(email); 

     ////send the message 

     System.Net.Mail.SmtpClient mySmtpClient = new System.Net.Mail.SmtpClient(); 
     System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("",""); 
     //mySmtpClient.Port="25"; 
     mySmtpClient.Host = ""; 
     mySmtpClient.UseDefaultCredentials = false; 
     mySmtpClient.Credentials = myCredential; 
     mySmtpClient.Send(mail); 



    if (string.IsNullOrEmpty(email)) 
    { 
     throw new Exception("You must supply an email address."); 
    } 

    if (string.IsNullOrEmpty(message)) 
    { 
     throw new Exception("Please provide a message to send."); 
    } 

    // If we get this far we know that they entered enough data, so 
    // here is where you would send the email or whatever you wanted 
    // to do :) 
} 

這應該是對你有好處:)

+0

非常感謝:) – 2012-04-05 14:27:31

0

難道你想讀從代碼隱藏文本框直接的價值?

mail.Body = TextBox1.Text; 
+0

這不工作對我來說,請檢查我的意見 – 2012-04-05 12:27:12

+0

否則我woudnt已經嘗試這種方法畢竟 – 2012-04-05 12:27:37

0

你輸入電子郵件地址TextBox2中,然後將其複製到HiddenField1但閱讀HiddenField2在代碼隱藏的地址。同樣,您將郵件正文輸入到TextBox1中,然後將其複製到HiddenField2中,但隨後讀取代碼隱藏中的HiddenField1(在客戶端上1和2之間的這種交換無需混淆)。在隱藏字段中測試值時,是否用數據填充兩個文本框?

0

首先,我認爲你應該刪除hiddenfields及其相關的任何東西(javascript函數)。重點關注導致問題的原因而不是解決方案,問題在於文本框值不會發布回服務器。當你最終得到它與隱藏領域的工作問題可能仍然存在。儘管您已經發現隱藏字段與原始文本框具有相同的問題。

隔離問題:

1)是實際值發送到服務器? (你可以通過開發工具中的瀏覽器或HttpWatch等插件來驗證,最糟糕的情況是你可以使用嗅探器)

2)確保updatepanels不會通過關閉ajax/updatepanel功能來干擾。 (您可以通過在您的ScriptManager上將EnablePartialRendering設置爲false來執行此操作,ScriptManager通常位於主頁上)。雖然這應該不重要。

3)如果您確認了正確的值實際發佈到服務器,請確保Page_Load(或類似功能)中沒有奇怪的邏輯覆蓋在OnLoad之前處理的回發值。這可能是一些初始化,只有當它不是回發時才應該運行。

4)處理回發後的變量時,確保存在控件,否則後退值將被忽略。你注意到代碼是在「CSS彈出窗口」中,確保在處理回發後變量時存在該彈出窗口(在控件集合中)。

+0

我已經停止使用asp.net按鈕,我正在使用提交按鈕,而不是 'var email = document.getElementById('HiddenField1 ')。值; var body = document.getElementById('HiddenField2')。value;'有沒有辦法將這個轉移到smtp處理 – 2012-04-05 13:44:48