Ive searched everywhere for this but cant find the answer. I have used the following code before to change repeated items properties in an asp.net repeater as after they have been bound to do things i couldnt do prior to binding them.
protected void rowRepeater_ItemBound(object sender, RepeaterItemEventArgs e)
{
foreach (RepeaterItem item in rptStockists.Items)
{
}
}
Now i am wanting to do something similar but with a chekbox list. i want to change the colour of the labels and i know i can only do this after the list has been bound. I have notice checkboxlists only provide the parameter OnDataBound in intellisense, doesnt give me OnItemDataBound that repeaters do. What would be the equivalent i could use here?
Well, you can use that DataBound event like:
protected void cbl_DataBound(object sender, EventArgs e)
{
foreach (ListItem item in cbl.Items)
{
}
}
Related
I have a datalist and I'm trying to apply a css class to a panel inside some of the items like so:
protected void DataListProducts_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header)
{
ProductsService service = new ProductsService();
DataTable dt = service.GetProduct(DataListProducts.DataKeys[e.Item.ItemIndex].ToString()).Tables[0];
if (((int)dt.Rows[0]["Quantity"]) <= 0)
{
Panel overlay = (Panel)DataListProducts.Items[e.Item.ItemIndex].FindControl("overlay");
overlay.CssClass = "soldOut";
}
}
}
When I try to run it I get the error
"Index was out of range. Must be non-negative and less than the size
of the collection."
I found out that the item index is equal to the item count of the datalist, which I think means the item hasn't been created yet, but shouldn't the ItemDataBound event fire after the item has been created and databound? Can someone please explain this to me?
Well, kinda solved it. I still don't know what the problem was, but iterating through the datalist with a foreach loop in a different function after the datalist has been databound gives me the desired effect.
I have a radcombobox,I want get checked items and save it in database but when i click save button,page is load again and my radcombobox become empty and then all of my checked items disappear.please help me,how can keep ckeckeditems?
As D Stanley mentioned in the comments, you're probably not checking for a postback when populating your dropdown.
This is the general approach you need to use in your code...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateTheDropdown();
}
}
private void PopulateTheDropdown()
{
// Populate / databind your dropdown here
}
This will ensure your dropdown is not rebound when a postback occurs so you don't lose the selected value(s).
If you have autopostback activated you must save the selected value separately. Try to explicitly disable and check the value when the event fires:
<telerik:RadComboBox ID="RadComboBoxControl" AutoPostBack="false" OnSelectedIndexChanged="RadComboBoxControl_SelectedIndexChanged" runat="server" EmptyMessage="Select something"></telerik:RadComboBox>
protected void RadComboBoxControl_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
// Only test
var seleccionado = RadComboBoxControl.Items.FindItemByText(e.Text);
}
Check if you have assigned DataSource of the control in other parts of the code
RadComboBoxControl.DataSource = ...
RadComboBoxControl.DataBind();
This will also lose the selected element
I have a piece of code that will add the selected item of a combobox to a listbox when a checkbox is checked. I would like to remove that selected item to be removed from the listbox when the checkbox is unchecked.
My problem is I cant simply repeat the code for removal to be the same as add because the combobox selection will different or empty when unchecked.
This is how it currently looks:
private void CBwasher_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
listBox2.Items.Add(comboBox1.SelectedItem);
}
if (checkBox1.Checked == false)
{
listBox2.Items.Remove(comboBox1.SelectedItem);
}
So I need a way of removing whatever was added by that check change instead of removing the selected index of the combobox. Please consider that there may be multiple lines in the listbox added by multiple different checkboxes.
You simply need to store the added item and remove it.
private object addedItem;
private void CBwasher_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
addedItem = comboBox1.SelectedItem;
listBox2.Items.Add(addedItem);
}
else
{
listBox2.Items.Remove(addedItem);
}
}
You may also need to check SelectedItem is null before adding/removing the item.
Focusing on the part where you said there might be multiple different checkboxes,
you need to store one item per checkbox.
You can write your own child class of the checbox control to add this feature, or simply use the Tag property.
You can also indicate which checkbox is linked to which combobox in the same way. Either child class or use the Tag property.
In my example, I'll assume you've referenced the combobox from the checkbox using the Tag property.
You can do it manually like this
checkBox1.Tag = comboBox1;
or hopefully you can automate it if you are generating these on the fly.
Here is the general idea of how the checkbox event should look.
The event is is utilising the sender argument, which means you should hook up all your checkboxes CheckedChanged events to this one handler. No need to create separate handlers for each.
private void CBwasher_CheckedChanged(object sender, EventArgs e)
{
var checkBox = (CheckBox)sender;
var comboBox = (ComboBox)checkBox.Tag;
if (checkBox.Checked && comboBox.SelectedItem != null)
{
listBox2.Items.Add(comboBox.SelectedItem);
comboBox.Tag = comboBox.SelectedItem;
}
if (!checkBox.Checked && comboBox.Tag != null)
{
listBox2.Items.Remove(comboBox.Tag);
}
}
Thanks to Peter Duniho's tip on how a Stack Overflow question has to look like, I converted my original question and its title hopefully to something more appropriate.
So I'm currently working on a conversation editor for a chatbot which consists of a treeview and two listviews. I store each treenode as a key(int) in a dictionary. The dictionary structure looks like this:
Dictionary<selectedNode(int), Tuple<List<listView1ItemLabels(string)>, List<listView2ItemLabels(string)>>>
Each list in the Tuple holds the labels of the items which are dynamically added to the two listviews at runtime using this custom function:
void AddItemToListView1(string itemName)
{
// add new Item to listView1 and
dictionary[selectedNodeID].Item1.Add(itemName);
// add its Text to the dictionary Tuple first generic list
listView1.Items.Add(itemName);
}
When I click a node (or rather "the corresponding dictionary key"), both listviews will be repopulated from the lists in the Tuple via the AfterSelect event of the treeView, which looks like this:
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
foreach(string str in dictionary[selectedNodeID].Item1)
{
listView1.Items.Add(str);
}
foreach(string str in dictionary[selectedNodeID].Item2)
{
listView2.Items.Add(str);
}
}
What I want to achieve is to change the string in the tuple's lists according to the change that happens to the listview item inside the AfterLabelEdit event. My approach below is obviously incorrect, even though the methods are valid:
private void listView1_BeforeLabelEdit(object sender, LabelEditEventArgs e)
{
// Capture the yet unedited label of the item
oldLabel = e.Label;
}
private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
if(e.Label == null)
return;
if(e.Label == "")
e.CancelEdit = true;
// remove the selected listview item label, which was previously added using the custom AddItem() function
dictionary[selectedNodeID].Item1.Add(e.Label);
dictionary[selectedNodeID].Item1.Remove(oldLabel);
}
I really don't see any reason why this won't work. What am I missing?
EDIT: Here's a picture of the GUI. May be it helps people understand what this is all about. :-)
I want to use a ComboBox with the DropDownList style (the one that makes it look like a button so you can't enter a value) to insert a value into a text box. I want the combobox to have a text label called 'Wildcards' and as I select a wildcard from the list the selected value is inserted in to a text box and the combobox text remains 'Wildcard'. My first problem is I can't seem to set a text value when the combobox is in DropDownList style. Using the properties pallet doesn't work the text value is simply cleared when you click off, adding comboBox.Text = "Wildcards"; to form_load doesn't work either. Can anyone help?
The code you specify:
comboBox.Text = "Wildcards";
...should work. The only reason it would not is that the text you specify is not an item within the comboBox's item list. When using the DropDownList style, you can only set Text to values that actually appear in the list.
If it is the case that you are trying to set the text to Wildcards and that item does not appear in the list, and an alternative solution is not acceptable, you may have to be a bit dirty with the code and add an item temporarily that is removed when the drop-down list is expanded.
For example, if you have a form containing a combobox named "comboBox1" with some items and a button named "button1" you could do something like this:
private void button1_Click(object sender, EventArgs e)
{
if (!comboBox1.Items.Contains("Wildcards"))
{
comboBox1.Items.Add("Wildcards");
}
comboBox1.Text = "Wildcards";
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
if (comboBox1.Items.Contains("Wildcards"))
comboBox1.Items.Remove("Wildcards");
}
That's pretty quick and dirty but by capturing the DropDownClosed event too you could clean it up a bit, adding the "Wildcards" item back as needed.
You can select one of items on formload or in form constructor:
public MyForm()
{
InitializeComponent();
comboBox.SelectedIndex = 0;
}
or
private void MyForm_Load(object sender, EventArgs e)
{
comboBox.SelectedIndex = 0;
}
Try this
comboBox1.SelectedValue = "Wildcards";
This may be a possible solution:
comboBox1.SelectedValue = comboBox1.Items.FindByText("Wildcards").Value;