Posted: 7/5/2011
Hey there!
Im developing website using asp.net and html mixed.
So I rather chose to use html select tag and checkbox tag. Contact.aspx.cs (behind code) doesn't recognise it because it is html tags. Is there a way to recognise html tags behind codes?? Look at below codes (bold):
Contact.aspx:
<select name="order" multiple="multiple" id="dlist1" onchange="swapImage1()"> Â Â Â Â Â Â <optgroup label="Sow Seeds of Love"> Â Â Â Â Â Â Â Â Â <option data-card="32.00" data-memo="62.00" data-tags1="1.80"Â data-tags4="8.50"Â data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/SowSeeds/Faith01_yellow.jpg">Faith Card</option> Â Â Â Â Â Â Â Â Â <option data-card="32.00" data-memo="62.00" data-tags1="1.80"Â data-tags4="8.50"Â data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/SowSeeds/Grace01_green.jpg">Grace Card</option>Â Â Â Â Â Â Â Â Â Â </optgroup> Â Â Â Â Â Â <optgroup label="Goosebumps Vintage A"> Â Â Â Â Â Â Â Â Â <option data-card="32.00" data-memo="62.00" data-tags1="1.80" data-tags4="----"Â data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/GoosebVA/GoosebumpVA_blue.jpg">Blue</option> Â Â Â Â Â Â </optgroup>
</select>
<table> Â Â Â Â Â Â <tr> Â Â Â Â Â Â Â Â Â <td class="style3"><input type="checkbox" name="description" value="Card A6" id="description" /> Card A6 (blank)</td> Â Â Â Â Â Â </tr>
</table>
Contact.aspx.cs:
 msg += "Name: " + txtName.Text; // fine        msg += "<br>Surname: " + txtSurname.Text; // fine        msg += "<br>Email: " + txtEmail.Text; // fine        msg += "<br>Order:" + HtmlTextWriterTag.Select.ToString("dlist1"); // belong to select tag
         msg += "<br>Order:" + HtmlTextWriterTag..... // belong to checkbox tag - how?
       mail.Body = msg;        mail.BodyEncoding = System.Text.Encoding.ASCII;
Hope you can help fix it! Your help much appreciated. It is urgent. Thank you!
Hi Natalie,
If you want html tags to be visible at code behind, you should add tag 'runat="server"', like this
<input type="checkbox" name="description" value="Card A6" id="description" runat="server" />
Best Regards,
Gjorgji
I followed your instruction. But when I debug it, error occurs. Look at this attachment.
I have inserted using System.Web.UI.HtmlControls; in code behind.
Why it not working? Sorry to bother you again but appreciate your value help.
Thanks for your time :-)
Â
Posted: 7/6/2011
Hi,You can get them via Request.Form["order"] and Request.Form["description"] Code:
<%@ Page Language="C#" AutoEventWireup="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <select name="order" multiple="multiple" id="dlist1" onchange="swapImage1()"> <optgroup label="Sow Seeds of Love"> <option data-card="32.00" data-memo="62.00" data-tags1="1.80" data-tags4="8.50" data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/SowSeeds/Faith01_yellow.jpg"> Faith Card</option> <option data-card="32.00" data-memo="62.00" data-tags1="1.80" data-tags4="8.50" data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/SowSeeds/Grace01_green.jpg"> Grace Card</option> </optgroup> <optgroup label="Goosebumps Vintage A"> <option data-card="32.00" data-memo="62.00" data-tags1="1.80" data-tags4="----" data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/GoosebVA/GoosebumpVA_blue.jpg"> Blue</option> </optgroup> </select> <table> <tr> <td class="style3"> <input type="checkbox" name="description" value="Card A6" id="description" /> Card A6 (blank) </td> </tr> </table> <asp:Button ID="SubmitButton" runat="server" Text="Button" /> </form> </body> </html> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { var order = Request.Form["order"]; var description = Request.Form["description"]; } </script>
Hey Raghav
Thanks for your reply. Appreciate it! Glad I dont get compile error but unfortnately it is not working after I copied your codes in the markup because I just checked my gmail account that the email didnt include this data (order and description). Sigh!
But the email only included the data (Name, surname and email address). Look at this screenshot picture.
Look my codes below (its working fine):
   protected void Send_Click(object sender, EventArgs e)    {
     string msg = "---------------------------<br>";
       msg += "Name: " + txtName.Text;        msg += "<br>Surname: " + txtSurname.Text;        msg += "<br>Email: " + txtEmail.Text;
       var order = Request.Form["order"];       var description = Request.Form["description"]; //I also have tried to move your codes in send_click event from page_load but it is also not working.
      string selectedValue = dlist1.Value; // I have tried another way even I put runat="server" attribute in the html tags - got compile error.
   }
I have been trying to several ways to solve it but still not this data in email. Strange! Thanks for your time :-)
I am sure the code I have given will work. Show your Page Load code and tell me where did you put my code?
I have put your code in the markup and it is not working then I put it in code behind - also not working (C#) :
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Net;using System.Net.Mail;public partial class _Default : System.Web.UI.Page{   protected void Page_Load(object sender, EventArgs e)   {       var order = Request.Form["order"];       var description = Request.Form["description"];   }   protected void Send_Click(object sender, EventArgs e)   {       string msg = "---------------------------<br>";       string from = "myemail@gmail.com"; //Replace this with your own correct Gmail Address       string to = "myemail@gmail.com"; //Replace this with the Email Address to whom you want to send the mail       MailMessage mail = new MailMessage();       mail.To.Add(to);       mail.From = new MailAddress(from, "Ellie Rose", System.Text.Encoding.UTF8);       mail.Subject = "Customer Order from Ellie Rose Website";       mail.SubjectEncoding = System.Text.Encoding.UTF8;       msg += "Name: " + txtName.Text;       msg += "<br>Surname: " + txtSurname.Text;       msg += "<br>Email: " + txtEmail.Text;       //string selectedValue = dlist1.Value;      // msg += "<br>Order:" + dlist1.SelectedIndex;      // msg += "<br>Order:" + HtmlTextWriterTag.Select.ToString("dlist1");      // msg += "<br>Description:" + HtmlTextWriterTag.Select.ToString("       mail.Body = msg;       mail.BodyEncoding = System.Text.Encoding.ASCII;       mail.IsBodyHtml = true;       mail.Priority = MailPriority.High;       SmtpClient client = new SmtpClient();       //Add the Creddentials- use your own email id and password       client.Credentials = new System.Net.NetworkCredential(from, "mypassword");       client.Port = 587; // Gmail works on this port       client.Host = "smtp.gmail.com";       client.EnableSsl = true; //Gmail works on Server Secured Layer       try       {           client.Send(mail);       }       catch (Exception ex)       {           Exception ex2 = ex;           string errorMessage = string.Empty;           while (ex2 != null)           {               errorMessage += ex2.ToString();               ex2 = ex2.InnerException;           }           HttpContext.Current.Response.Write(errorMessage);       } // end try        Response.Redirect("Thanks.aspx");   }}
If there is no way to solve it, maybe i can send my zip file to you so that you can see what is wrong if you dont mind. If you refuse, i will understand :-) If that is okay with you, can you send your email address to me so that I can email attachment to you. My email address is natzjordaan@gmail.com
I hate to disturb you but thanks for your patience! Thank you very much, again!
I wonder how are you passing values of order and description which you set at page load to your click event. You can go like this:
private string order { get; set; } private string description { get; set; } protected void Page_Load(object sender, EventArgs e) { order = Request.Form["order"]; description = Request.Form["description"]; }
and then use them in your click event.Â
Yay thank you it is working but one select tag is not working. It is too close to be fixed :-D I have been exploring and found something.
I got the email from gmail account - look at the data (bold):
Name: NatalieSurname: JordaanEmail: cyberoxy@lantic.net
Description:Card A6 // displaying data yay it is working :-DÂ
Order:true  // no data
Is your problem resolved or not can you be specific what is not resolved?
Seem you dont get my other message. I resend it:
Yes the one problem (description) is resolved. Yay thank you very much! But one select tag is not working. It is too close to be fixed :-D I have been exploring and found something.
Name: xxxSurname: xxxEmail: xxxxx
Description:Card A6 // displaying data yay it is working :-D
 Order:true  // no data
It should be : Order: Images/order_names/faithcard.jpg
code behind:
With the below code I am getting order as "Images/Order_names/SowSeeds/Grace01_green.jpg"
<%@ Page Language="C#" AutoEventWireup="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <select name="order" multiple="multiple" id="dlist1" onchange="swapImage1()"> <optgroup label="Sow Seeds of Love"> <option data-card="32.00" data-memo="62.00" data-tags1="1.80" data-tags4="8.50" data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/SowSeeds/Faith01_yellow.jpg"> Faith Card</option> <option data-card="32.00" data-memo="62.00" data-tags1="1.80" data-tags4="8.50" data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/SowSeeds/Grace01_green.jpg"> Grace Card</option> </optgroup> <optgroup label="Goosebumps Vintage A"> <option data-card="32.00" data-memo="62.00" data-tags1="1.80" data-tags4="----" data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/GoosebVA/GoosebumpVA_blue.jpg"> Blue</option> </optgroup> </select> <table> <tr> <td class="style3"> <input type="checkbox" name="description" value="Card A6" id="description" /> Card A6 (blank) </td> </tr> </table> <asp:Button ID="SubmitButton" runat="server" Text="Button" /> </form> </body> </html> <script runat="server"> private string order { get; set; } private string description { get; set; } protected void Page_Load(object sender, EventArgs e) { order = Request.Form["order"]; description = Request.Form["description"]; } </script>
Dear Raghav, I have used your code script in markup but it keeps displaying compile error so I tried another way to solve it and it is working but I am just asking you what must I replace witth Visible to display data such as "Images/Order_names/SowSeed/Grace01_green.jpg" instead of displaying "true"?
Email in my gmail account:
 Order:true  // no data - how can I display full data "Images/Order_names/SowSeed/Grace01_green.jpg"??
Please review my codes below:
Show me your full markup which you have right now.
Okay I copy both markup and code behind:
Markup (remember Im using masterpage):
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Contact.aspx.cs" Inherits="_Default" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">Â Â Â <link href="CSS/StyleSheet.css" rel="stylesheet" type="text/css" />Â Â Â <script src="Java/Prices.js" type="text/javascript"></script>Â Â Â <script src="Java/Images1.js" type="text/javascript"></script>Â Â Â </asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">Â Â Â <div id="OrderContent"><p>Please complete the following details & we will be in touch as soon as possible:</p>Â Â Â <form id="Form1" method="post" enctype="text/plain" runat="server">Â <table>Â <tr>Â Â Â <td>Â Â Â Â Â <label>Name:</label>Â Â Â </td>Â Â Â <td>Â Â Â Â Â <label><asp:TextBox ID="txtName" runat="server" placeholder="" required Width="210px"></asp:TextBox></label>Â Â </td>Â </tr>Â <tr>Â Â Â <td>Â Â Â Â Â <label>Surname:</label></td>Â Â Â <td>Â Â Â Â Â <label><asp:TextBox ID="txtSurname" runat="server" placeholder="" required Width="210px"></asp:TextBox></label>Â Â Â </td>Â </tr>Â <tr>Â Â Â <td>Â Â Â Â Â <label>E-mail:</label>Â Â Â </td>Â Â Â <td>Â Â Â Â Â <label><asp:TextBox ID="txtEmail" runat="server" placeholder="" required Width="210px"></asp:TextBox></label>Â Â Â </td>Â </tr>Â <tr></table>Â Â Â Â Â Â Â Â <table><tr>Â <td>Â <p>Order the following:</p><select name="order" multiple="multiple" size="12" style="width:330px;" id="dlist1" onchange="swapImage1()"><optgroup label="Sow Seeds of Love" id="group" runat="server"> //Instead of select tag because I got no compile error and even I got data in email like order="true" it almost working but i need full data like order:Images/Order_names/SowSeeds/Faith01_yellow.jpg instead of "true"Â Â Â Â Â Â Â Â Â <option data-card="32.00" data-memo="62.00" data-tags1="1.80"Â data-tags4="8.50"Â data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/SowSeeds/Faith01_yellow.jpg">Faith</option>Â Â Â Â Â Â Â Â Â <option data-card="32.00" data-memo="62.00" data-tags1="1.80"Â data-tags4="8.50"Â data-key="30.00" data-fridge="40.00" data-flower_sachet="----" value="Images/Order_names/SowSeeds/Grace01_green.jpg">Grace</option></optgroup></select>Â </td>Â <td>Â Â Â <img class="ImageToSwap" id="imageToSwap1" src="Images/NoneImage.gif" width="147" height="195" />Â </td></tr></table>Â Â Â <table>Â Â Â <tr>Â Â Â Â Â Â Â <td>Description:</td>Â Â Â Â Â Â Â <td class="style1">Price (ZAR):</td>Â Â Â </tr></table><table>Â Â Â Â Â Â <tr>Â Â Â Â Â Â Â Â Â <td class="style3"><label><input type="checkbox" name="description" value="Card A6" id="description" runat="server" /> Card A6 (blank)</label></td>Â Â Â Â Â Â Â Â Â <td id="price_card"></td>Â Â Â Â Â Â </tr>Â Â Â Â Â Â <tr>Â Â Â Â Â Â Â Â Â <td class="style3"><label><input type="checkbox" name="description" value="Memo Book A6" id="description" runat="server" /> Memo Book A6</label></td>Â Â Â Â Â Â Â Â Â <td id="price_memo"></td>Â Â Â Â Â Â </tr></table><br />Â <p><asp:Button ID="Send" runat="server" Text="Send Email" onclick="Send_Click" Â Â Â Â Â Â Â Â style="height: 26px"/><input type= "reset" value="Clear" /></p></form></div></asp:Content>
Code behind (C#):
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Net;using System.Net.Mail;public partial class _Default : System.Web.UI.Page{ // private string order { get; set; }  // private string description { get; set; }   protected void Page_Load(object sender, EventArgs e)   {   }   protected void Send_Click(object sender, EventArgs e)   {       string msg = "---------------------------<br>";       string from = "XXXX@gmail.com"; //Replace this with your own correct Gmail Address       string to = "XXXXX@gmail.com"; //Replace this with the Email Address to whom you want to send the mail       MailMessage mail = new MailMessage();       mail.To.Add(to);       mail.From = new MailAddress(from, "Ellie Rose", System.Text.Encoding.UTF8);       mail.Subject = "Customer Order from Ellie Rose Website";       mail.SubjectEncoding = System.Text.Encoding.UTF8;       msg += "Name: " + txtName.Text;       msg += "<br>Surname: " + txtSurname.Text;       msg += "<br>Email: " + txtEmail.Text;       msg += "<br>Order:" + group.Attributes.Add("order", "Images/Order_names/..."); // it is from optgroup tag (i already included runat="server" and id="group") I tried to change it instead of group.Visible; (only display "true") but not working.       msg += "<br>Description:" + description.Value; //description.Checked;       mail.Body = msg;       mail.BodyEncoding = System.Text.Encoding.ASCII;       mail.IsBodyHtml = true;       mail.Priority = MailPriority.High;       SmtpClient client = new SmtpClient();       //Add the Creddentials- use your own email id and password       client.Credentials = new System.Net.NetworkCredential(from, "mypassword");       client.Port = 587; // Gmail works on this port       client.Host = "smtp.gmail.com";       client.EnableSsl = true; //Gmail works on Server Secured Layer       try       {           client.Send(mail);       }       catch (Exception ex)       {           Exception ex2 = ex;           string errorMessage = string.Empty;           while (ex2 != null)           {               errorMessage += ex2.ToString();               ex2 = ex2.InnerException;           }           HttpContext.Current.Response.Write(errorMessage);       } // end try        Response.Redirect("Thanks.aspx");   }}