Cannot Get the HTML textbox value

2019-09-02 06:38发布

问题:

I have an html form that goes like this:

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

Now, What I wanted to do is to get the user's input into the textboxes once they push the send button.. For now I have an aspx integrated to this markup so it can send to an email. Here it is :

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

For the record, it is already sending the email once the button in the html is pushed but it only sends the hard coded string in my c# code and it doesnt get the value of the other textboxes.

Can you help me out ?

回答1:

try to get it by name

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


回答2:

use runat="server" for each control you need to access from code behind

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

then you can get the value as below

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