繁体   English   中英

获取CheckBoxList的选定值

[英]Get selected values of a CheckBoxList

所以我从CheckBoxList获取值时遇到了这个问题-我能够从选定的值中获取“文本”-但是使用值并不是那么容易。

我要实现的是,对于选定的值,有一个数字,对于每个选定的值,我都希望将其值乘以其选定的值。 如果这有意义吗?

到目前为止,我完整的代码是:

{
    MailMessage mail = new MailMessage();
    SmtpClient client = new SmtpClient();

    client.Port = 587;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    client.DeliveryFormat = SmtpDeliveryFormat.International;
    client.Timeout = 10000;
    client.Credentials = new System.Net.NetworkCredential("myemail@mail.com", "myPassword");

    if (this.fiUpload1.HasFile)
    {
        this.fiUpload1.SaveAs(Server.MapPath("MyAttach/" + fiUpload1.FileName));
        mail.Attachments.Add(new Attachment(Server.MapPath("MyAttach/" + fiUpload1.FileName)));
    }
    //Gets the selected text from the checkboxlist
    string items = string.Empty;
    foreach (ListItem i in checkedUsers.Items)
    {
        if (i.Selected == true)
        {
            items += i.Text + ",";
        }
    }

    mail.To.Add(new MailAddress("mail@mail.com"));
    mail.From = new MailAddress("mail@mail.com");
    mail.Subject = txtSubject.Text;
    mail.Body = "Ekstra informasjon fra kunde : " + txtBody.Text + " Kunden sine valgte produkter for bestilling : " + items;

    client.Send(mail);

    Label1.Text = "Bestilling sendt ! ";
}

因此,正如我们对于mail.body所看到的,它已经设法获取选定的“文本”或“项目”来对其进行调用。 如上所述,我也希望它包含每个选定项的值,并将它们加在一起得出总数。

我用SumString.Join (用逗号连起来)的LINQ查询更具可读性:

var selectedItems = checkedUsers.Items.Cast<ListItem>()
    .Where(li => li.Selected)
    .Select(li => int.Parse(li.Text));
int sum = selectedItems.Sum();
string items = string.Join(",", selectedItems);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM