I have a CheckBoxList and 5 labels.
I would like the text value of these Labels to be set to the 5 selections made from the CheckBoxList after the user clicks on a button. How would I get this accomplished?
Thanks in advance.
bind an event to a button,
iterate trough the Items property of the CheckBoxList
set the text value according to the selected property of the listitem
like:
protected void button_Click(object sender, EventArgs e)
{
foreach (ListItem item in theCheckBoxList.Items)
{
item.Text = item.Selected ? "Checked" : "UnChecked";
}
}
to add a value you could do:
foreach (ListItem item in theCheckBoxList.Items)
{
item.Text = item.Selected ? item.Value : "";
}
or display al values in a mini-report:
string test = "you've selected :";
foreach (ListItem item in theCheckBoxList.Items)
{
test += item.Selected ? item.Value + ", " : "";
}
labelResult.Text = test;
find selected items from CheckboxList by Lambda Linq:
var x = chkList.Items.Cast<ListItem>().Where(i => i.Selected);
if (x!=null && x.Count()>0)
{
List<ListItem> lstSelectedItems = x.ToList();
//... Other ...
}
Why don't you have one label and on the button click do something like:
foreach (var li in CheckList1.Items)
{
if(li.Checked)
Label1.Text = li.Value + "<br />";
}
That may not be the exact syntax but something along those lines.
Use this in LINQ:
foreach (var cbx3 in CheckBoxList2.Controls.OfType<CheckBox>().Where(cbx3 => cbx3.ID == s))
{
cbx3.Checked = true;
}
Related
I need to to use a checkboxlist because there is a unique case the user is allowed to select 3 items at the same time. Problem is that I can't even manage to get the single selection to work. I'm doing this is codebehind with an updatepanel to not refresh the page.
protected void cblCodeRequest_OnSelectedIndexChanged(object sender, EventArgs e)
{
int count = 0;
int i = 0;
int maxObjects = cblCodeRequest.Items.Count;
string[] checkObjects = new string[maxObjects];
ListItem selectedItem = new ListItem();
foreach (ListItem item in cblCodeRequest.Items)
{
checkObjects[i] = item.Text;
i++;
}
foreach (ListItem item in cblCodeRequest.Items)
{
if (item.Selected)
{
count++;
selectedItem = item;
cblCodeRequest.ClearSelection();
}
foreach (ListItem itm in cblCodeRequest.Items)
{
if (item.Equals(selectedItem))
{
item.Selected = true;
}
}
}
}
I'm not sure how to proceed from here, i already save the selected item and clear the whole selection and then set it again but it doesn't do it, it just selects itself again after even if i click a different checkbox. I think my logic is messed up
Try this, Your foreach statements are not arranged correctly
protected void cblCodeRequest_OnSelectedIndexChanged(object sender, EventArgs e)
{
ListItem selectedItem = cblCodeRequest.Items[cblCodeRequest.SelectedIndex]
cblCodeRequest.ClearSelection();
int x = 0;
for(x; x<cblCodeRequest.Items.Count; x++)
{
if (cblCodeRequest.Items[x].Equals(selectedItem))
{
item.Selected = true;
}
}
}
Ok, I finally solved this problem. I had to dig through the problem with the debugger and follow the logic. I did this in jQuery so I can remove the update panel. If Anyone ever gets the same problem as me, this is the solution for you.
This allows you to use a checkboxlist like a radiobuttonlist but also allows you to check multiple checkboxes that belong to a special selection group.
$(function () {
$('[id*=cblCodeRequest] input').on('click', function () {
var checkboxlist = $('[id*=cblCodeRequest]'); // CheckBoxList
var checkboxArray = $('[id*=cblCodeRequest] input'); // CheckBoxList Items
var current = $(this); // Get Selected Checkbox
var label = $(current).next().text(); // Get CheckBox label name
// Uncheck every checkBox that don't match the unique multi selection combo
if (label != "Recâmbio" && label != "Série" && label != "Embalagem Alternativa") {
// Loop through checkboxArray and uncheck every item that does not meet the condition
$(checkboxArray).each(function (i) {
$(this).prop("checked", false);
});
// Check the selected item again
$(current).prop("checked", true);
} else {
// Get the current checkbox name
var chk = $(current).next().text();
// If the checkbox that matches the unique combo selection was click uncheck all invalid checkboxes that don't match
if (chk == "Série" || chk == "Recâmbio" || chk == "Embalagem Alternativa") {
// loop through the checkboxlist again
$(checkboxArray).each(function () {
// Get the looped item name
var ck = $(this).next().text();
// if checkbox does not belong to the unique combo selection, uncheck it
if (ck != "Recâmbio" && ck != "Série" && ck != "Embalagem Alternativa") {
$(this).prop("checked", false);
}
});
}
}
});
});
i am trying to add the selected items from the listbox to the textbox with the comma seperated between each other. but it is only reading the first element of the selected items every time.if i select three values holding ctrl its only passing the fist elemnt of selected items
if (ListBox1.SelectedItem != null)
{
// int count = ListBox1.SelectedItems.Count;
if (TextBox1.Text == "")
TextBox1.Text += ListBox1.SelectedItem.ToString();
else
TextBox1.Text += "," + ListBox1.SelectedItem.ToString();
}
if listbox contain :1,2,3,4
example output inside textbox: 1,1,1,1
expected output: 1,2,3,4 (for evry selection it shouldnt display the already selected value again)
var selectedItemText = new List<string>();
foreach (var li in ListBox1.Items)
{
if (li.Selected == true)
{
selectedItemText.Add(li.Text);
}
}
Then
var result = string.Join(selectedItemText,",");
The ListBox has a SelectedItems property, that you can iterate over:
foreach (var item in ListBox1.SelectedItems)
{
TextBox1.Text += "," + item.ToString();
}
At the end you need to remove the first "," as it will be in front of the first items string representation:
TextBox1.Text = TextBox1.Text.Substring(1, TextBox1.Text.Legth - 1);
Try this
var selected = string.Join(",", yourListBox.Items.GetSelectedItems());
public static class Extensions
{
public static IEnumerable<ListItem> GetSelectedItems(
this ListItemCollection items)
{
return items.OfType<ListItem>().Where(item => item.Selected);
}
}
i have a dropdown list which i want to highlight a item from it. i have given the condition correct as par to me.But it didnt highlight the given item,instead showing normally as other items.
DataTable dtt = new DataTable();
dtt.Load(cmd.ExecuteReader());
ddlCompanyName.DataSource = dtt;
ddlCompanyName.DataTextField = "COMPANYNAME";
ddlCompanyName.DataValueField = "COMPANYID";
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
ddlCompanyName.DataBind();
ddlCompanyName.Items.Insert(0, new ListItem("--Select Name--"));
Compidd(string) has specified item to be highlighted in dropdownlist
You need to do DataBind before the loop:
ddlCompanyName.DataBind();
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
EDIT:
To set the value as default value you can try like this
ddlCompanyName.SelectedValue = "The value which you want to set as default"
The ddlCompanyName.DataBind(); must be executed before you loop the items:
ddlCompanyName.DataBind();
foreach (ListItem item in ddlCompanyName.Items)
{
if (item.Text == compidd)
{
item.Attributes.Add("style", "background-color:#3399FF;color:white;font-weight:bold;");
}
}
Otherwise there are no items in the DropDownList.
I have set visible property of my menuStrip1 items to false as
foreach (ToolStripMenuItem itm in menuStrip1.Items)
{
itm.Visible = false;
}
Now I know the Names of toolStripMenuItem and dropDownItem of the menustrip1. How to can I activate the required toolStripMenuItem and dropDownItem.
I have
string mnItm = "SalesToolStripMenuItem";
string ddItm = "invoiceToolStripMenuItem";
Now I want to set visible true to these two(toolStripMenuItem and dropDownItem) items. How can I do that? I know those names only.
Simply use those names to get the actual item via MenuStrip.Items indexer:
ToolStripMenuItem menuItem = menuStrip1.Items[mnItm] as ToolStripMenuItem;
ToolStripDropDownMenu ddItem = menuStrip1.Items[ddItm] as ToolStripDropDownMenu;
You can use
menuStrip1.Items[mnItm].Visible = true;
menuStrip1.Items[ddItm].Visible = true;
or if you want to set Visible to multiple toolstrip items:
string [] visibleItems = new [] {"SalesToolStripMenuItem", "invoiceToolStripMenuItem"};
foreach (ToolStripMenuItem item in menuStrip1.Items)
{
if (visibleItems.Contains(item.Name))
{
item.Visible = false;
}
}
Hope it helps
You're looking for ToolStripItemCollection.Find method.
var items = menustrip.Items.Find("SalesToolStripMenuItem", true);
foreach(var item in items)
{
item.Visible = false;
}
second parameter says whether or not to search the childrens.
You should try something like this:
string strControlVal ="somecontrol"; //"SalesToolStripMenuItem" or "invoiceToolStripMenuItem" in your case
foreach (ToolStripMenuItem item in menuStrip1.Items)
{
if (strControlVal == item.Name)
{
item.Visible = false;
}
}
Initialize strControlVal string on your discretion where you need it.
If i get your question you are trying to disable other than the above two mentioned toolstrip items. Since you know the name of the menu items a slight change in code can get you along
foreach (ToolStripMenuItem itm in menuStrip1.Items)
{
if(itm.Text !="SalesToolStripMenuItem" || itm.Text !="invoiceToolStripMenuItem")
{
itm.Visible = false;
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
string MenuItemName = sender.ToString()
}
I have a list-box, I want to loop through all the selected items and get each selected items text value.
for (int i = 0; i < lstFieldNames.selectedItems.Count; i++)
{
string s = lstFieldNames.SelectedItems[i].ToString();
}
the value of s is "{ Item = ADDR }"
I don't need the { Item = }, I just want the text "ADDR".
What am I doing wrong, I tried a few things and nothing seems to work for me.
Well, this is a Winforms question because an ASP.NET ListBox has no SelectedItems property(notice the plural). This is important since a Winforms ListBox has no ListItems with Text and Value properties like in ASP.NET, instead it's just an Object.
You've also commented that the datasource of the ListBox is an anonymous type. You cannot cast it to a strong typed object later.
So my advice is to create a class with your desired properties:
class ListItem {
public String Item { get; set; }
}
Create instances of it instead of using an anonymous type:
var items = (from i in xDoc.Descendants("ITEM")
orderby i.Value
select new ListItem(){ Item = i.Element("FIELDNAME").Value })
.ToList();
Now this works:
foreach (ListItem i in lstFieldNames.SelectedItems)
{
String item = i.Item;
}
Note that my ListItem class is not the ASP.NET ListItem.
string s = lstFieldNames.SelectedItems[i].Text.ToString();
Note the Text property
This would fetch Selected values only.
EDIT:
foreach (object listItem in listBox1.SelectedItems)
{
string s = listItem.ToString();
}
Use this code :
string s = "";
foreach(var item in lstFieldNames.SelectedItems)
{
s += item.ToString() + ",";
}
textBox1.Text = s;
If need on one selected item from your listbox try this cod :
string s = "";
foreach(var item in lstFieldNames.SelectedItems)
{
s = item.ToString();
}
EDIT :
Try this one code
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = "";
foreach (var item in listBox1.SelectedItems)
{
s += item.ToString() + ",";
}
textBox1.Text = s;
}