2012-04-05 46 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