本文介绍了将复选框中的项目填充到文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从复选框中获取所选项目的列表
I am getting the list of selected items from the checkbox
public string getselecteditems(CheckBoxList CheckBoxList1)
{
var items = new StringBuilder();
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
items.Append(string.Format("{0},", item.Value));
}
return items.ToString().TrimEnd(',');
}
我正在尝试填充复选框中选中的这个项目。
And I am trying to populate this item selected in checkbox.
private void populateselecteditems(string selecteditems, CheckBoxList CheckBoxList1)
{
if (!string.IsNullOrWhiteSpace(selecteditems))
{
string[] items = selecteditems.Split(',');
foreach (ListItem item in CheckBoxList1.Items)
{
item.Selected = items.Where(c => c.Equals(item.Value))
.FirstOrDefault() != null;
}
}
}
请帮帮我将此值分配给文本框(当我尝试下面的内容时,它会显示
Please help me how to assign this value to a textbox ( when I try the below, it shows
protected void Button1_Click(object sender, EventArgs e)
{
// Textbox1.Text = populated values should be here.
//Textbox1.Text = populateselecteditems(selecteditems ,CheckBoxList CheckBoxList1);
}
</pre>
推荐答案
private string populateselecteditems(string selecteditems, CheckBoxList CheckBoxList1)
{
if (!string.IsNullOrWhiteSpace(selecteditems))
{
string[] items = selecteditems.Split(',');
foreach (ListItem item in CheckBoxList1.Items)
{
item.Selected = items.Where(c => c.Equals(item.Value))
.FirstOrDefault() != null;
}
}
return selecteditems;
}
protected void Button1_Click(object sender, EventArgs e)
{
// Textbox1.Text = populated values should be here.
//Textbox1.Text = populateselecteditems(selecteditems ,CheckBoxList CheckBoxList1);
Textbox1.Text=populateselecteditems(selecteditems ,CheckBoxList CheckBoxList1);
}
protected void TextToCheckbox_Click(object sender, EventArgs e)
{
populateselecteditems(TextBox1.Text, CheckBoxList1);
}
protected void btnCheckboxtoTextbox_Click(object sender, EventArgs e)
{
TextBox1.Text = getselecteditems(CheckBoxList1);
}
private void populateselecteditems(string selecteditems, CheckBoxList CheckBoxList1)
{
if (!string.IsNullOrWhiteSpace(selecteditems))
{
string[] items = selecteditems.Split(',');
foreach (ListItem item in CheckBoxList1.Items)
item.Selected = items.FirstOrDefault(k => k == item.Text) != null;
}
}
public string getselecteditems(CheckBoxList CheckBoxList1)
{
string output = "";
CheckBoxList1.Items.OfType<ListItem>().Where(k => k.Selected).ToList().ForEach(k => output += k + ",");
return output.Trim(',');
}
这篇关于将复选框中的项目填充到文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!