值没有从javascript传递到codebehind asp.net?

时间:2012-04-05 12:22:16

标签: c# javascript asp.net c#-4.0 smtp

    <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代码,我试图首先从文本框转移到隐藏字段然后代码隐藏

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

时,会显示值

但是它们没有传递给codebehind

 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弹出窗口中,这是主要问题

  4. AS建议我使用html按钮调用javascript函数

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

    如何将此值作为我在Asp.net中的凭据传递

    请帮忙

4 个答案:

答案 0 :(得分: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 :)
}

这对你有好处:)

答案 1 :(得分:0)

你不想在代码隐藏时直接从TextBox读取值吗?

mail.Body = TextBox1.Text;

答案 2 :(得分:0)

您正在将电子邮件地址输入TextBox2,然后将其复制到HiddenField1,但在代码隐藏中读取HiddenField2以获取地址。类似地,您正在将邮件正文输入到TextBox1中,然后将其复制到HiddenField2中,然后在代码隐藏中读取HiddenField1(这在客户端上的1和2之间交换是不必要的混淆)。在隐藏字段中测试值时,是否要在两个文本框中填入数据?

答案 3 :(得分:0)

首先,我认为你应该删除隐藏的字段和与之相关的任何内容(javascript函数)。关注导致问题的原因不是解决方案,问题是文本框值不会发布回服务器。当您最终使用隐藏字段时,问题可能仍然存在。虽然您已经发现隐藏字段与原始文本框具有相同的问题。

隔离问题:

1)值是否实际发布到服务器? (您可以使用开发人员工具中的浏览器构建或像HttpWatch这样的插件来验证这一点,在最坏的情况下,您可以使用嗅探器)

2)通过关闭ajax / updatepanel功能,确保更新面板不会干扰。 (您可以通过在ScriptManager上将EnablePartialRendering设置为false来执行此操作,ScriptManager通常位于母版页上)。虽然这不重要。

3)当您确认实际发布到服务器的正确值时,请确保Page_Load(或类似函数)中没有奇怪的逻辑覆盖在OnLoad之前处理的回发值。这可能是一些初始化,只有在它不是回发时才会运行。

4)确保处理回发后变量时控件存在,否则忽略后置值。您注意到代码位于“CSS弹出窗口”中,确保在处理回发后变量时,此弹出窗口(在控件集合中)存在。