select items in a checkbox list based on their text - c#

i have a check box list in my asp.net page ...i need to select the check box based on their text...am getting these string values form the database and storing it in a array.....the below code works fine for a single text ..What should i do in case of array..how should i pass the array values in the if loop
for (int i = 0; i < chkbx.Items.Count; i++)
{
if (chkbx.Text == "Dress" )
{
chkbx.Items[i].Selected = true;
}
}

Below code should work for you -
string[] array = { "Dress", "Pen", "Table"};
for (int i = 0; i < chkbx.Items.Count; i++)
{
if (array.Contains(chkbx.Items[i].Text))
{
chkbx.Items[i].Selected = true;
}
}

You probably want to use the Contains method of the array that contains your text values:
for (int i = 0; i < chkbx.Items.Count; i++)
{
if (myArray.Contains(chkbx.Items[i].Text))
{
chkbx.Items[i].Selected = true;
}
}
where myArray is the array of values you're populating from the database.

The following is a nice way of doing it
foreach (string item in myarr)
{
checkboxlist1.Items[checkboxlist1.Items.IndexOf(checkboxlist1.Items.FindByText(item))].Selected = true;
}

Related

String list contains of all sub-components

String list G is :
[0] : {"1,2,5"}
[1] : {"1,2,4,5,6"}
[2] : {"2,4,6"}
[3] : {"1,4,6"}
With the following commands, we conclude that "1,4" exists in the List G[3] :
if (G[i].Contains("1,4")) { //code here }
How to modify the above commands, that in addition feature (Contains), "1,4" exists in the List G[1]?
Program codes
for (int i = 0; i < candid.Count; i++)
{
foreach (TransactionTP b in transactions)
{
string search = candid[i];
var searchNumbers = search.Split(',').Select(int.Parse).ToList();
for (int j = 0; j < G.Count; j++)
{
IEnumerable<int> numbers = G[j].Split(',').Select(int.Parse);
int idx = 0;
foreach (var number in numbers)
{
if (number == searchNumbers[idx])
{
idx++;
}
if (idx == searchNumbers.Count)
{
arraye[i] = arraye[i] + (b.transactionUtility);
break;
}
}
}
}
}
Update:
The order of the searched term matters.
In order to preserve the ordering of the set you are matching go (4,1 in this case), you will need to evaluate each string, keeping track of where you are in the match.
string[] G = new[]
{
"1,2,5",
"1,2,4,5,6",
"2,4,6",
"1,4,6"
};
string search = "1,4";
var searchNumbers = search.Split(',').Select(int.Parse).ToList();
for (int i = 0; i < G.Length; i++)
{
// Convert the string into an enumeration of numbers
IEnumerable<int> numbers = G[i].Split(',').Select(int.Parse);
// Index to keep track of the search
int idx = 0;
// Loop through the input set sequentially
foreach (var number in numbers)
{
// Check if the input matches the next expected number
if (number == searchNumbers[idx])
{
idx++;
}
if (idx == searchNumbers.Count)
{
Console.WriteLine("String {0} matched", G[i]);
break;
}
}
}
Replace your single Contains call with a method that does this:
bool passes = false;
for(int i = 0; i < G.Length; i++)
{
List<string> temp = new List<string>();
if(G[i].Contains(","))
{
temp = G[i].Split(",");
}
else
{
temp = G[i];
}
if(temp.Contains("1") && temp.Contains("4")
{
passes = true;
}
}
return passes;
This eliminates the possibility of matching on "10" or "41" etc etc
It also will not care if your elements are sorted, even though you said they are. It will also match regardless of the number of entries between "1" and "4" in the list.
You could spice this up to take any number of inputs that you want to match before qualifying as a match, I'll leave that to you if you want to make it so.

Select listbox item if contain a string in asp.net using c#

I'm trying to select listbox item if contains a specific string. Here is my code:
string SearchDomain="youdial.in";
for (int i = 0; i < ListBox2.Items.Count; i++)
{
var UrlList = new Uri(ListBox2.Items[i].ToString());
var UrlList = UrlList.Host;
if (UrlList == SearchDomain)
{
ListBox2.SelectedIndex = i;
urllbl.Text = ListBox2.SelectedItem.ToString();
return;
}
}
If ListBox2 have more than 1 matched results than above code select last matched item, but i want to select first matched item. e.g if Query match with 3rd, 7th and 9th item, I want to get the value of 3rd item. Thanks in Advance, sorry for my bad English.
Just change return; to break; Break statement will stop the for loop from executing further.
string SearchDomain="youdial.in";
for (int i = 0; i < ListBox2.Items.Count; i++)
{
var UrlList = new Uri(ListBox2.Items[i].ToString());
var UrlList = UrlList.Host;
if (UrlList == SearchDomain)
{
ListBox2.SelectedIndex = i;
urllbl.Text = ListBox2.SelectedItem.ToString();
break;
}
}
Just reverse the counter change the
for (int i = 0; i < ListBox2.Items.Count; i++)
to
for (int i = ListBox2.Count-1 ; i > -1 ; i--)

How do I bind a non database column value to repeater label using asp.net

I have a little challenge that I can't get around. I have a result from the code below.
if (Request.Cookies["ProductRecord"] != null)
{
s = Convert.ToString(Request.Cookies["ProductRecord"].Value);
string[] strArr = s.Split('|');
for (int i = 0; i < strArr.Length; i++)
{
t = Convert.ToString(strArr[i].ToString());
string[] strArr1 = t.Split(',');
for (int j = 0; j < strArr1.Length; j++)
{
a[j] = strArr1[j].ToString();
}
SingleCount = (Convert.ToDecimal(a[1].ToString()) * Convert.ToDecimal(a[3].ToString()));
}
}
How do I bind the results of SingleCount to a label inside a repeater.
RepeaterId.DataSource = SingleCount is the closest i have thought of but its not working.
I'm making some assumptions based upon your code so I may be off-base on what you're trying to achieve, but couldn't you take SingleCount and add it to a list of decimals and then bind that list to the DataSource of your repeater?

having problems with checklistbox click events

This code works but the results show up when I check and then uncheck again in the first checkbox. I want to check only once.Does anyone have an opinion? Thanks!
private void clb_grup_MouseClick(object sender, MouseEventArgs e)
{
Liste.Items.Clear();
string s = "";
foreach (var item in clb_kisiler.CheckedItems)
{
s = s + item.ToString() + " ";
}
string[] secilenler = s.Split(' ');
if (clb_grup.GetItemChecked(0) == true)
{
for (int x = 0; x < clb_kisiler.Items.Count; x++)
{
clb_kisiler.SetItemChecked(x, true);
}
for (int i = 0; i < clb_kisiler.Items.Count; i++)
{
Liste.Items.Add(clb_kisiler.Items[i].ToString());
}
}
else if (clb_grup.GetItemChecked(1) == true)
{
for (int x = 0; x < clb_idari.Items.Count; x++)
{
clb_idari.SetItemChecked(x, true);
}
for (int i = 0; i < clb_idari.Items.Count; i++)
{
Liste.Items.Add(clb_idari.Items[i].ToString());
}
}
else if (clb_grup.GetItemChecked(2) == true)
{
for (int x = 0; x < clb_teknik.Items.Count; x++)
{
clb_teknik.SetItemChecked(x, true);
}
for (int i = 0; i < clb_teknik.Items.Count; i++)
{
Liste.Items.Add(clb_teknik.Items[i].ToString());
}
}
foreach (object i in clb_kisiler.CheckedItems)
{
Liste.Items.Add(i.ToString());
}
}
NEW CODE
My friend used string arrays instead of the combo boxes.She is also having the same problem as me,and she also tried to write a code that would prevent dublicate items being showed up in the list box.She has one checked list box the same as me,her second checked list box is empty,and one list box.Heres the code.
private void chklstbx_bolum_ItemCheck(object sender, ItemCheckEventArgs e)
{
//event for the first check list box
string[] tumu = { "Jane", "Tammy", "John", "Emily", "Susan", "Julie", "Amelia", "Katherine" };
string[] idari = { "Julie", "Amelia", "Katherine" };
string[] teknik = { "Jane", "Tammy", "John", "Emily", "Susan" };
if (chklstbx_bolum.GetItemChecked(0) == true)
{
//if the first box in the first check list box is checked then do this
//this part of the code works fine but it doesnt check if there are //duplicates of the same name in the list box
chklstbx_sonuc.Items.Clear();
for (int i = 0; i < 5;i++ )
{
chklstbx_sonuc.Items.Add(teknik[i]);
chklstbx_sonuc.SetItemChecked(i, true);
lstbx_sonuc.Items.Add(teknik[i]);
}
}
else if (chklstbx_bolum.GetItemChecked(1) == true){
//do this if the second box in the first check box list is checked
//Here the program checks to see if there are duplicates when adding from the second check list box but the program just freezes.
chklstbx_sonuc.Items.Clear();
int x=0;
do
{
for (int i = 0; i < 3; i++)
{
chklstbx_sonuc.Items.Add(idari[i]);
chklstbx_sonuc.SetItemChecked(i, true);
lstbx_sonuc.Items.Add(idari[i]);
}
} while ( x== 0);
x++;
for (int t = 0; t < lstbx_sonuc.Items.Count; t++)
{
for (int s = 0; s < chklstbx_sonuc.Items.Count; s++)
{
if (chklstbx_sonuc.Items[s].ToString() != lstbx_sonuc.Items[t].ToString())
lstbx_sonuc.Items.Add(chklstbx_sonuc.Items[s]);
}
}
}
}
So now the second question is why does the second else if statement create a problem and makes the program not respond? And we still want to know how are we able to transfer the items in the string array without having to check and uncheck but directly transfer when its checked?
The click event is not the right one. you should use the check event and then verify in the event arguments if the checkbox has been checked or unchecked.
EDIT
I've read your other problem. There is an easier solution than manually checking which has been checked.
You need to assign a value to each checkbox with the ID of the list you want to transfer. You can then check in your code which value has been checked (likely through the sender) and use FindControl(checkboxvalue) to find the list that you want to transfer. Then you first transfer all the items, and then you check them. As I mentioned above, you also need to verify in your checkedeventargs (or whatever the eventargs parameter of the checked event is) if the checkbox is currently checked or unchecked.

Can't solve enum related display options in booking service

I've got a booking service in which you can select by an enum if you want to display all seats, all available seats or all booked seats.
My problem is that I don't know how to make it so i.e. only the booked seats are shown, because as of now if I select "only booked seats" it shows the correct number of reserved seats, but it iterates from the beginning of the entire array instead of the ones that I want, so if there are 3 reserved seats, it will show seat 0,1,2 instead of the ones that are actually reserved.
I am pretty sure that I need to change the for (int i = 0; i < count; i++) to for (int i = 0; i < totalNumberOfSeats; i++) instead to actually loop through all seats instead of just as many as there are of the kind that I want to display, but then I get out of bound exception and I don't know how to proceed.
public string[] GetSeatInfoStrings(DisplayOptions choice)
{
int count = GetNumOfSeats(choice);
if (count <= 0)
{
return null;
}
string[] strSeatInfoStrings = new string[count];
for (int i = 0; i < count; i++)
{
strSeatInfoStrings[i] = GetSeatInfoAt(i);
}
return strSeatInfoStrings;
}
public string GetSeatInfoAt(int index)
{
int row = GetRow(index);
int col = GetCol(index);
string seatInfo;
if (string.IsNullOrEmpty(GetName(m_nameMatrix[row, col])))
{
seatInfo = MarkAsVacant(row, col);
}
else
{
seatInfo = MarkAsReserved(row, col);
}
return seatInfo;
}
I've got a method IsReserved(int index) and tried something like this
if (IsReserved(i))
{
// Want to return all seats that are reserved
}
else if (!IsReserved(i))
{
// Want to return all seats that are NOT reserved
}
As far as for that method working, it is okay, but the problem is that I don't know what to put within the brackets.
This is a question where we cant help without knowing your complete model. There is little detail. May be you want something like this:
string[] strSeatInfoStrings = new string[count];
int counter = 0;
for (int i = 0; i < totalNumberOfSeats; i++)
{
var seatInfo = GetSeatInfoAt(i);
if (seatInfo == "reserved") //some kind of checking
{
strSeatInfoStrings[counter] = seatInfo;
counter++; //run another counter
}
}
return strSeatInfoStrings;
You can avoid all the hassle of array and counter and just use a List<T>..
var strSeatInfoStrings = new List<string>();
for (int i = 0; i < totalNumberOfSeats; i++)
{
var seatInfo = GetSeatInfoAt(i);
if (seatInfo == "reserved") //some kind of checking
strSeatInfoStrings.Add(seatInfo);
}
return strSeatInfoStrings;
It's probably easier to use a List than an array in this case, because with a List you don't need to know the size before you start adding to it.
public string[] GetSeatInfoStrings(DisplayOptions choice)
{
List<string> lstSeatInfoStrings = new List<string>();
for (int i = 0; i < totalNumberOfSeats; i++)
{
string seatInfo = GetSeatInfoAt(i);
if (seatInfo.Reserved)
{
lstSeatInfoStrings.Add(seatInfo);
}
}
if (lstSeatInfoStrings.Count == 0)
{
return null;
}
return lstSeatInfoStrings.ToArray();
}

Categories