【问题标题】:Values not getting passed from javascript to codebehind asp.net?值没有从 javascript 传递到代码隐藏 asp.net?
【发布时间】:2012-04-05 12:22:16
【问题描述】:
    <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 中的凭据传递

请帮忙

【问题讨论】:

  • 为什么不简单地在代码隐藏中使用 TextBox 值?
  • 我试过它没有工作同样的错误所以尝试使用同样的事情也发生在这里
  • 这是在 UserControl 中还是在您的代码示例中可能不明显?
  • @jrummell 我认为可能导致提交时值不存在的唯一控件是UpdatePanel。但是,在这种情况下,似乎 OP 正在使用链接提交,这是行不通的。
  • 我也试过了,最初也没有使用更新面板

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


【解决方案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 :)
}

这应该对你有好处:)

【讨论】:

    【解决方案2】:

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

    mail.Body = TextBox1.Text;
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        隔离问题:

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

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

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

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

        【讨论】:

        • 我已停止使用 asp.net 按钮,我使用的是提交按钮,而不是 'var email = document.getElementById('HiddenField1').value; var body = document.getElementById('HiddenField2').value;'有没有办法将其转移到 smtp 处理
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-30
        • 1970-01-01
        • 2013-01-14
        相关资源
        最近更新 更多