I have asked this question previously but for VB.NET here:
Accessing buttons names using variables
Now I want to do the same but in C# and with CheckBoxes so for example, if I have 31 check boxes labeled "CheckBox1...CheckBox31" I could do :
for (int i = 0; i < 10; i++)
{
(CheckBox + i).Enabled = false;
}
Thanks for any suggestions.
Try following
for (int i = 0; i < 10; i++)
{
((CheckBox)this.Controls[$"CheckBox{i}"]).Enabled = false;
}
You can try creating a List<CheckBox> or an array of all checkboxes:
for (int i = 0; i < checkbox_array.Length; i++) {
checkbox_array[i].Enabled = false;
}
EDIT: I might be a bit late, but if you put all CheckBoxes in a GroupBox (wich I really recommend doing if you have soo many checkboxes), you can just loop trough all the controls in that groupbox like this:
foreach (CheckBox cbx in gbxCheckBoxes.Controls) {
cbx.Enabled = true;
}
or like this: (if you only need to enable them)
gbxCheckBoxes.Enabled = false;
(gbxCheckBoxes is the GroupBox I was talking about)
Related
for(int i = 1; i < 5;i++)
{
label[i].InnerText = info.books[0].title;
}
I want something like this to access id's of 5 server controls in loop.
label1,label2,.....,label5. It is a code behind file.
If you really wanted you can use FindControl
for (int i = 1; i < 5; i++)
{
Label lbl = Page.FindControl("Label" + i) as Label;
lbl.Text = info.books[0].title;
}
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?
I am new on devexpress,so I have a simple question here.what is the equal of the this code on devexpres gridview.
here is the my code
for (int i = 0; i <gridView1.RowCount; i++)
{
string Name = gridView1.Rows[i].["ColumName"].toString();
}
I looked at the a method something gridView1.Rows[i] but ,not available.
Take a look at the documentation for Traversing Rows in the DevExpress support center. Looks like this would work:
for (int i = 0; i < gridView1.VisibleRowCount; i++)
{
DataRow row = gridView1.GetDataRow(i);
string name = row["ColumnName"].ToString();
}
You can loop through like this
for (int i = 0; i < gridView1.VisibleRowCount; i++)
{
int currentRowHandle = gridView1.GetVisibleRowHandle(i);
string value = grid.GetCellValue(rowHandle,grid.Columns["Column Name"]);
}
I have checkboxes named CheckBox1,CheckBox3,CheckBox5,CheckBox7,CheckBox9,CheckBox11. I wants to iterate through these check boxes and wants to disable them. I have written like this
for (int i=1; i < 12; i++)
{
((CheckBox)(i.ToString())).Enabled = false;
i=i+2;
}
but this is not the proper way. please help
Try using FindControl like this if you have id of controls like Checkbox1,Checkbox3 etc.
for (int i = 1; i < 12; i+=2)
{
CheckBox cb = (CheckBox)Page.FindControl("Checkbox" + i);
cb.Enabled = false;
}
Your code would like to set the enabled=false for checkboxes that have an odd number at the end of their id? Then this will do that
foreach (Control ctrl in Page.Controls)
{
if (ctrl is CheckBox && ctrl.id.length > 9)
{
int chkboxNumber = int.Parse(ctrl.id.SubStr(9));
if ( chkboxNumber % 2 == 1) // Check for odd numbers
{
(CheckBox)ctrl.Enabled = false;
}
}
}
May this help you.
foreach (Control c in Page.Controls)
{
if (c is CheckBox)
{
CheckBox myCheckBox = (CheckBox)c;
myCheckBox.Enabled = false;
}
}
This will disable you all CheckBoxes you have.
Edit: Sorry didnt see the +2 at the i. My fault.
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;
}