2011-03-22 59 views
0

我在ASP.NET C#中創建一個表單,以便可以填寫並通過電子郵件發送給多個收件人。表單的一部分是一個複選框部分,其中包含多個選項。我只能選擇第一個選項通過電子郵件發回給收件人,所以如果用戶選擇兩個或多個複選框,它只會通過電子郵件發送第一個選項。以下是我的代碼單ASP.NET複選框控件

SmtpClient smtpClient = new SmtpClient(); 
    MailMessage message = new MailMessage(); 
    MailAddress From = new MailAddress(mailTextBox.Text); 
    message.To.Add(new MailAddress("[email protected]")); 
    message.Subject = (companyTextBox.Text); 
    message.IsBodyHtml = true; 
    message.Body = "<html><head></head><body>" + 
    "<p></p>" + 
    "<p>Business Type: " + typeDropDownList.Text + "</p>" + 
    "<p>Company: " + companyTextBox.Text + "</p>" + 
    "<p>Name: " + nameTextBox.Text + "</p>" + 
    "<p>Address: " + addressTextBox.Text + "</p>" + 
    "<p>City: " + cityTextBox.Text + "</p>" + 
    "<p>State: " + stateDropDownList.Text + "</p>" + 
    "<p>Zip Code: " + zipcodeTextBox.Text + "</p>" + 
    "<p>Phone Number: " + phoneTextBox.Text + "</p>" + 
    "<p>Email: " + mailTextBox.Text + "</p>" + 
    "<p>Number Of Locations: " + locationsDropDownList.Text + "</p>" + 

    **// This is my problem area //** 
    "<p>Interested In: " + interestedCheckBoxList.Text + "</p>" + 
    "<p>Interested In: " + interestedCheckBoxList.Text + "</p>" + 
    "<p>Interested In: " + interestedCheckBoxList.Text + "</p>" + 
    **// This is my problem area //** 

    "<p>Message: " + messageTextBox.Text + "</p>" + 
    "</body></html>"; 
    smtpClient.Send(message); 
    Response.Redirect("http://www.domain.com"); 

在此先感謝您。

吉姆

+0

你正在採取相同的3行代碼並複製它。 – Cyberdrew 2011-03-22 18:38:56

+2

我也推薦使用StringBuilder。 – Cyberdrew 2011-03-22 18:39:43

+0

此外,CheckBox控件和asp.net中的CheckBoxList控件有所不同。小心不要讓他們困惑。 – 2011-03-22 18:41:07

回答

1

你需要通過你的CheckBoxList迭代,並找到所有的檢查項目,並得到了Text財產的每個項目和附加到你的電子郵件文本。

string yourSelectedList = ""; 
foreach (ListItem i in chklst.Items) 
{ 
    if (i.Selected) 
     yourSelectedList += (i.Text + ", "); 
} 

那麼,在年底刪除多餘的逗號:)

"<p>Interested In: " + yourSelectedList + "</p>" + 

嘗試concatentating許多串在一起時使用StringBuilder,因爲它會帶來很大的區別。

2

您需要遍歷CheckBoxList中的Items並逐個添加它們。

例子:

foreach(ListItem li in interestedCheckBoxList.Items) 
{ 
    //add your stuff 
    if(li.Selected) 
    { 
     //should be using string builder here but.... 
     message.Body += "<p>Interested In: " + li.Text + "</p>"; 
    } 
} 
0

嘗試用以下替換代碼在您的「問題區域」:

string InterestedIn = ""; 
foreach (ListItem li in interestedCheckBoxList.Items) 
{ 
    if (li.Selected) 
     InterestedIn += "<p>Interested In: " + li.Text + "</p>"; 
} 

當然,你不能連接這是你原來的字符串連接的一部分,因此請構建一個「InterestedIn」字符串並將電子郵件正文連接起來。