-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 代码,我试图先从文本框传输值,然后传输到隐藏字段,然后是代码隐藏

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

在此处输入图像描述

编辑:

  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 中的凭据传递

请帮忙

4

4 回答 4

1
<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 :)
}

这应该对你有好处:)

于 2012-04-05T14:24:30.763 回答
0

您不想直接从代码隐藏上的 TextBox 读取值吗?

mail.Body = TextBox1.Text;
于 2012-04-05T12:26:13.357 回答
0

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

于 2012-04-05T12:58:24.863 回答
0

首先,我认为您应该删除隐藏字段和与之相关的任何内容(javascript 函数)。专注于导致问题的原因而不是解决方案,问题是文本框值没有回发到服务器。当您最终让它与隐藏字段一起工作时,问题可能仍然存在。尽管您已经发现隐藏字段与原始文本框存在相同的问题。

隔离问题:

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

2) 通过关闭 ajax/updatepanel 功能确保更新面板不会干扰。(您可以通过在通常位于母版页上的 ScriptManager 上将 EnablePartialRendering 设置为 false 来做到这一点)。虽然这应该无关紧要。

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

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

于 2012-04-05T13:34:12.953 回答