繁体   English   中英

无法获取HTML文本框值

[英]Cannot Get the HTML textbox value

我有这样的html形式:

 <form id="main-contact-form" name="contact-form" method="post" action="test.aspx">
                            <div class="form-group">
                                <input id="txtname" type="text" class="form-control" placeholder="Name" required>
                            </div>
                            <div class="form-group">
                                <input id="txtemail" type="email" class="form-control" placeholder="Email" required>
                            </div>
                            <div class="form-group">
                                <input id="txtsubject" type="text" class="form-control" placeholder="Subject" required>
                            </div>
                            <div class="form-group">
                                <textarea id="txtmessage" class="form-control" rows="8" placeholder="Message" required></textarea>
                            </div>
                            <button type="submit" class="btn btn-default">Send Message</button>
                        </form>

现在,我想做的就是在用户按下发送按钮后将其输入文本框。现在,我将一个aspx集成到该标记中,以便可以发送至电子邮件。 这里是 :

MailMessage mail = new MailMessage ();
        mail.From = new System.Net.Mail.MailAddress ("username@domain.com");
        // The important part -- configuring the SMTP client
        SmtpClient smtp = new SmtpClient ();
        smtp.Port = 587;   // [1] You can try with 465 also, I always used 587 and got success
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
        smtp.UseDefaultCredentials = false; // [3] Changed this
        smtp.Credentials = new NetworkCredential ("username@domain.com", "password");  // [4] Added this. Note, first parameter is Email Address of the sender and the next parameter is the password.
        smtp.Host = "smtp.gmail.com";

        //recipient address 
        mail.To.Add (new MailAddress ("user1@gmail.com"));
        mail.To.Add (new MailAddress ("user2@gmail.com"));

        //Formatted mail body 
        mail.IsBodyHtml = true;
        mail.Body = Request.Form ["txtmessage"];
        mail.Subject = Request.Form ["txtsubject"];
        string st = "This is a Test  Message" ;

        mail.Body = st;
        smtp.Send (mail);

记录一下,一旦按下html中的按钮,它已经发送了电子邮件,但是它只发送了我的c#代码中的硬编码字符串,并且没有获取其他文本框的值。

你能帮我吗 ?

尝试按名称获取

<input id="txtname" name="txtname" type="text" class="form-control" placeholder="Name" required>

对您需要从后面的代码访问的每个控件使用runat="server"

<input id="txtemail"  runat="server" type="email" class="form-control" placeholder="Email" required>

那么你可以得到如下值

 mail.From = new System.Net.Mail.MailAddress(txtemail.Value);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM