selecting multiple values in checkboxlist not working [Resolved]

Posted by Parthibansk under ASP.NET on 5/9/2013 | Points: 10 | Views : 4613 | Status : [Member] | Replies : 8
i got checkboxlist in contactus form..

if user selects multiple items form checkboxlist..it has to send mail..

but i get only 1 selected value from checkboxlist..

how to solve below is code:


string s1 = string.Empty;
foreach (ListItem item in this.CheckBoxList1.Items)
{
if (item.Selected)
{

s1 = item.ToString();


MailMessage mm = new MailMessage("xxx@xyz.com", "parthibansk@yahoo.com");
// mm.Subject = txtSubject.Text;
mm.Body = "Name: " + TXTNAME.Text + "<br /><br />ADDRESS: " + TXTADDRS.Text + "<br /><br/>EMAIL:" + TXTEMAIL.Text + "<br/><br/>PHONENO:" + TXTPHONE.Text + "<br /><br/>PROJECTS:" + CheckBoxList1.Items.FindByValue(s1);
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.net4india.com";
smtp.EnableSsl = false;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "xxxxx@xyz.com";
NetworkCred.Password = "xxxxxxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 25;
smtp.Send(mm);
Label1.Text = "Email Sent SucessFully.";

}

}
}




Responses

Posted by: Kmandapalli on: 5/10/2013 [Member] Silver | Points: 50

Up
0
Down

Resolved
Hi,

Check it.....

protected void Button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
string s1 = string.Empty;
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
s1 = item.ToString();
sb.Append(s1);
sb.Append("|");
}
}

MailMessage mm = new MailMessage();
mm.From = new MailAddress("smtpmail747@gmail.com");
mm.To.Add(new MailAddress(txtEmail.Text));
mm.Body = sb.ToString();
mm.IsBodyHtml = true;
SmtpClient s = new SmtpClient("smtp.gmail.com");
s.Port = 587;
s.EnableSsl = true;
s.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
s.Credentials = new System.Net.NetworkCredential("smtpmail747@gmail.com", "knowledgeinfote");
s.Send(mm);
Label1.Text = "Email Sent SucessFully.";
}

Mark as answer if satisfied...........


Regards,
Shree M.

Kavya Shree Mandapalli

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 5/9/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

Try this.......

In your .aspx page, write the following code.

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Email :"></asp:Label>
&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>Kavya</asp:ListItem>
<asp:ListItem>Bharathi</asp:ListItem>
<asp:ListItem>Govind</asp:ListItem>
<asp:ListItem>Sarath</asp:ListItem>
<asp:ListItem>Prabhu</asp:ListItem>
<asp:ListItem>Sreenath</asp:ListItem>
<asp:ListItem>MallaReddy</asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="Submit"
onclick="Button1_Click" />

Next, in the .aspx.cs file write the following code.

protected void Button1_Click(object sender, EventArgs e)
{
string s1 = string.Empty;
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
s1 = item.ToString();

MailMessage mm = new MailMessage();
mm.From = new MailAddress("smtpmail747@gmail.com");
mm.To.Add(new MailAddress(txtEmail.Text));
mm.Body = CheckBoxList1.Items.FindByValue(s1).ToString();
mm.IsBodyHtml = true;
SmtpClient s = new SmtpClient("smtp.gmail.com");
s.Port = 587;
s.EnableSsl = true;
s.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
s.Credentials = new System.Net.NetworkCredential("smtpmail747@gmail.com", "knowledgeinfote");
s.Send(mm);
Label1.Text = "Email Sent SucessFully.";
}
}
}

Its working.....

Mark as Answer if its helpful to you.........

Regards,
Shree M.

Kavya Shree Mandapalli

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Parthibansk on: 5/9/2013 [Member] Starter | Points: 25

Up
0
Down
MULTIPLE VALUES NOT SELECTING.. I CHECKED

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 5/10/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

I have implemented the code and its working for me.
I have attached the zip file, try it.........

Regards,
Shree M.
 Download source file

Kavya Shree Mandapalli

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Parthibansk on: 5/10/2013 [Member] Starter | Points: 25

Up
0
Down
downlaoded and run..getting two mails.. one for selected value1 and other for 2nd selcted value

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kmandapalli on: 5/10/2013 [Member] Silver | Points: 25

Up
0
Down
Hi,

The code that you wrote will give you that output only.
Tell me clearly what exactly is your requirement...

Mark as Answer if satisfied ......

Regards,
Shree M.

Kavya Shree Mandapalli

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Parthibansk on: 5/10/2013 [Member] Starter | Points: 25

Up
0
Down
these are items in my checkboxlist.. if i select multiple items in checkboxlist..it has to get selected...


Projects

Esperanza
Belvista
Miraya Rose
Miraya Garden
Miraya Villas

shown as below:

PROJECTS:|Miraya Rose||Alila Bangalore

Mira

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Parthibansk on: 5/10/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks a lot.. i got it..

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response