簡體   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