I have 14 Dropdownlist accroding to 7 Days.
Like For a day First dropdown list is Named as From-Time and Second Dropdown list is To-Time.
List Values are Set by 30 minutes Time Difference.
To-Time dropdownlist Should save only those list items which fall after From-time Dropdownlist.like if i select 1 Pm by first then second dropdown list should carry list-items after 1 Pm.
removing is done like this..
protected void ddlMonst_SelectedIndexChanged(object sender, EventArgs e)
{
RemoveListItem(sender as DropDownList,checkboxes);
}
private void RemoveListItem(DropDownList DDl,DropDownList[] checkboxes)
{
int CurrrentSelectedIndex = DDl.SelectedIndex;
String StartDDlName = DDl.ID.Substring(3, 3).ToString() + "ed";
String TargetedDDlName = string.Empty;
for (int i = 0; i < checkboxes.Length; i++)
{
TargetedDDlName = checkboxes[i].ID.Substring(3, 5).ToString() ;
if (StartDDlName.Equals(TargetedDDlName))
{
for(int j=0 ;j<CurrrentSelectedIndex;j++)
checkboxes[i].Items.RemoveAt(0);
}
}
}
but this logic fails if i selected again and again from First Dropdown list.It reomoves all from second one.
How to Handle this situation
Assuming your code is successfully removing the Items, then it makes sense that the To-Time DropDowns could end up being empty after multiple successive selects from the From-Time DropDown(s).
Instead of using .RemoveAt(), you could .Clear() your To-Time DropDowns and then .Add() the Items from the From-Time DropDown from its SelectedIndex and onwards.
// ...
// get reference to To/From DropDown(s) here
// ...
dd_time_to.Items.Clear();
for (int i = dd_time_from.SelectedIndex; i < dd_time_from.Items.Count; i++)
{
dd_time_to.Items.Add(dd_time_from.Items[i]);
}
You'll have to modify this to work on your DropDownList[]
Related
I'm trying to create previous and next buttons on a page to navigate through items in a drop down list. Clicking next should select the next item in the DDL and clicking previous should go to the previous item.
Here's something I've attempted for the next button but it just takes me to the last row.
protected void btnNext_Click(object sender, EventArgs e)
{
int currentSelection = DDL.SelectedIndex;
for (int i = currentSelection; i < DDL.Items.Count; i++)
{
string nextSelection = (DDL.Items[i].ToString());
DDL.SelectedValue = nextSelection;
}
}
You're looping through all the items in the list until you get to the last one, and selecting each item individually until the loop exits. Drop a breakpoint inside that loop and debug to see what I mean.
You don't need any looping here at all. What you'd want instead would be simply:
int nextIndex = DDL.SelectedIndex + 1;
if (nextIndex + 1 >= DDL.Items.Count)
return; // We're on the last item, do nothing (or whatever you like)
DDL.SelectedValue = DDL.Items[nextIndex].ToString();
You easily can change selected value by its index so you don't need change it by its value.
int i = ddl.SelectedIndex;
if (i+1!=ddl.Items.Count)
{
ddl.SelectedIndex = i + 1;
}
else
{
ddl.SelectedIndex = i;
}
as you said in comment it wont change index if the current index is the last one.
When I checked items on list in checkedListBox1 and search some items using textBox1 my previous check is gone. When i search using textBox1 and check some item on list and search another item previous checked is gone too. Any solutions? C#
void ladujZBazy(string mustContains)
{
checkedListBox1.Items.Clear();
listSurowceTabela.Clear();
indexes.Clear();
bazaproduktowDBEntities dc = new bazaproduktowDBEntities();
var c1 = from d in dc.SurowceTabela select d.NazwaSurowca;
var c2 = from d in dc.SurowceTabela select "(" + d.PartiaSurowca + ")";
var c3 = from d in dc.SurowceTabela select d.IloscSurowca;
var c4 = from d in dc.SurowceTabela select d.JednostkaSurowca;
listSurowceTabela.Add(c1.ToList());
listSurowceTabela.Add(c2.ToList());
listSurowceTabela.Add(c3.ToList());
listSurowceTabela.Add(c4.ToList());
for (int i = 0; i < listSurowceTabela[0].Count; i++)
{
string strToAdd = "";
for (int j = 0; j < listSurowceTabela.Count; j++)
{
strToAdd += " " + listSurowceTabela[j][i] + " ";
}
if (mustContains == null)
{
checkedListBox1.Items.Add(strToAdd);
indexes.Add(i);
}
else if (strToAdd.ToLower().Contains(mustContains.ToLower()))
{
checkedListBox1.Items.Add(strToAdd);
indexes.Add(i);
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
ladujZBazy(textBox1.Text);
}
Comment out the Clear() methods at the top of your code. This should demonstrate that you are clearing those values. Then work your way through what actually needs to be cleared and what does not.
Well, your problem basically resides at these lines in ladujZBazy():
checkedListBox1.Items.Clear();
indexes.Clear();
where you are calling to clear all the contents of the checkedListBox1 and it's stored indexes.
So, every call made to your function, you are clearing up all the contents from the checkedListBox1 and then recreating/appending contents back to it. Hence, it is just refreshing all the items in the checkedListBox1(i.e. removing any already checked items in the list.).
So, we have 2 ways by which we can make it work.
We can add a Boolean parameter to the ladujZBazy() which will determine whether to clear the checkedListBox1 or not.
And your modified ladujZBazy() will then look something like this:
void ladujZBazy(string mustContains, bool dropIndexes)
{
// the below code will only run the value is supplied as TRUE
if(dropIndexes)
{
checkedListBox1.Items.Clear();
listSurowceTabela.Clear();
indexes.Clear();
}
// your rest of the code goes here
}
And then call it from your textbox's TextChanged event as:
ladujZBazy(textBox1.Text,false);// pass TRUE to clear the checked items
Or we can move the index and checked item Clearing and CheckListBox Refreshing Logic to a separate function as:
private void refreshChkListBox()
{
checkedListBox1.Items.Clear();
listSurowceTabela.Clear();
indexes.Clear();
//your code to append items to list goes here
}
And later call this function to refresh the CheckListBox if you have a need for it.
So basically I am using a FOR loop to add and remove options from a listbox.
It functions correctly when selected 1 option (from either remove or select) and it functions correctly when I select two separate options (For example, item[0] and item[4]).
However, I am having trouble when I try to select all options or when I select two items that are side by side ([2],[3].. etc)
Here is my loop for the select function:
protected void btnSelect_Click(object sender, EventArgs e)
{
for (int intCounter = 0; intCounter < lbSnacks.Items.Count; intCounter++)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSelected.Items.Add(lbSnacks.Items[intCounter]);
}
}
for (int intCounter = 0; intCounter < lbSnacks.Items.Count; intCounter++)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSnacks.Items.Remove(lbSnacks.Items[intCounter]);
}
}
}
The error is basically taking the item and putting it into the "selected" listbox but it is leaving behind one of the two options in the original "snacks" box.
Any ideas?
The problem is, when you remove an item, all of the other items shift downwards - which means the next loop iteration (since it increments your index) "skips" one item.
There are various ways to handle this. The simplest is to just loop backwards:
for (int intCounter = lbSnacks.Items.Count-1; intCounter >= 0; intCounter--)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSelected.Items.Add(lbSnacks.Items[intCounter]);
lbSnacks.Items.Remove(lbSnacks.Items[intCounter]);
}
}
This way, when the items "shift", it doesn't matter, since you've already dealt with those items.
I have two list boxes and an ASP button. I am trying to use the button to move only the first item in the index of one list box to the other. Unfortunately whenever the button is pushed, it either moves all of the items in the list box or moves the first one in the index as many times as there are items (for example: if there are five items in the list box, it will move the first item to the next list box 5 times. so i have no items in the first list box and five of the first item in the next list box.) Also, the list boxes are in an Ajax update panel (I don't know if that is relevant). Here is the code:
protected void btnMoveFirst_Click(object sender, EventArgs e)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
ListBox2.Items.Add(ListBox1.Items[0]);
}
for (int i = ListBox1.Items.Count - 1; i >= 0; i--) {
ListBox1.Items.Remove(ListBox1.Items[0]);
}
}
Replace your code with this code
protected void btnMoveFirst_Click(object sender, EventArgs e)
{
//Add items only once
ListBox2.Items.Add(ListBox1.Items[0]);
ListBox1.Items.Remove(ListBox1.Items[0]);
}
In your code you are adding item till the count of items in ListBox1
for (int i = 0; i < ListBox1.Items.Count; i++)
{
ListBox2.Items.Add(ListBox1.Items[0]);
}
That's why it is adding your items more number of times
i want the code to disable default selection in dropdownlist in asp.net and also on selection of a particular data field the values are displayed in the txtbox below.the dropdown should be filled with system related data like eg c:drive, d ... etc dynamically at run time.
then you can manually append ""--select--"" in the first place in the dropdownlist by edit items. and set appenddatabounditems to true.. thus you will get select in the first place rather than the first drive by default......
ok then use this function and cal this function on the databound event of dropdownlist.
void RemoveDuplicateItems(DropDownList ddl)
{
for (int i = 0; i < ddl.Items.Count; i++)
{
ddl.SelectedIndex = i;
string str = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
{
ddl.SelectedIndex = counter;
string compareStr = ddl.SelectedItem.ToString();
if (str == compareStr)
{
ddl.Items.RemoveAt(counter);
counter = counter - 1;
}
}
}
}
and call this function in dropdownlist_databound() event.
then you can manually append ""--select--"" in the first place in the dropdownlist by edit items. and set appenddatabounditems to true.. thus you will get select in the first place rather than the first drive by default......