Вопрос

Whats the best way for me to change the ID's of the check boxes, like as in using a loop, the problem I'm having is with the current control ID CheckBox1.ID, i cant seem to change the 1 to be used as a variable

CheckBox1.ID = "chckbx_1";
CheckBox2.ID = "chckbx_2";
CheckBox3.ID = "chckbx_3";
CheckBox4.ID = "chckbx_4";
CheckBox5.ID = "chckbx_5";
CheckBox6.ID = "chckbx_6";

Is there any way where i can implement this logic?, and please note, I'm using web forms

Это было полезно?

Решение

Try something like this:

for (int i = 1; i < some_number; i++) 
{    
    Control myControl = FindControl("CheckBox" + i.ToString());

    if(myControl != null && myControl.GetType() == typeof(CheckBox)) 
    {
        ((CheckBox)myControl).ID = "chckbx_" + i.ToString();
        ((CheckBox)myControl).CssClass = "newClass";
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top