How do I loop this

I

iHavAQuestion

I actually need to do the below for 10 controls

ddlInsuranceCompany1.SelectedItem.Text = Value;
txtPolicy1.Text = value;

SO i opted for a for loop

My question is how do i add contol + increment
ddlInsuranceCompany + integer

The below code does do not work can anyone plz hep me out

for (int iCount = 0; iCount < 10; iCount++)
{
int iControls = iCount + 1;
ddlInsuranceCompany + (iControls).SelectedItem.Text
= ilist.Tables[0].Rows[iCount]["InsurComp_TEXT"].ToString();
txtPolicy+ (iControls).Text =
ilist.Tables[0].Rows[iCount]["PoliciesNum_INT"].ToString();
}
 
S

Scott M.

You can't concatenate a number to a control name to come up with a control
name that will be recognized by the compiler, since the compiler checks your
variable names before the concatenation happens.

Instead, put all the controls into a collection (like a panel) and loop
through the controls in the panel. You can then check the type of control
you're iterating over to know whether it's a textbox or a dropdown list.

You'd use a foreach loop to accomplish this.

-Scott
 
G

Göran Andersson

iHavAQuestion said:
I actually need to do the below for 10 controls

ddlInsuranceCompany1.SelectedItem.Text = Value;
txtPolicy1.Text = value;

SO i opted for a for loop

My question is how do i add contol + increment
ddlInsuranceCompany + integer

The below code does do not work can anyone plz hep me out

for (int iCount = 0; iCount < 10; iCount++)
{
int iControls = iCount + 1;
ddlInsuranceCompany + (iControls).SelectedItem.Text
= ilist.Tables[0].Rows[iCount]["InsurComp_TEXT"].ToString();
txtPolicy+ (iControls).Text =
ilist.Tables[0].Rows[iCount]["PoliciesNum_INT"].ToString();
}

You can just put the references in arrays, so that you can access them
by index:

DropDownList[] dropdowns = new DropDownList[] { ddlInsuranceCompany1,
ddlInsuranceCompany2, ..., ddlInsuranceCompany10 };
TextBox[] textboxes = new TextBox[] { txtPolicy1, txtPolicy2, ... ,
txtPolicy10 };
for (int i = 0; i < 10; i++) {
dropdowns.SelectedItem.Text =
ilist.Tables[0].Rows["InsurComp_TEXT"].ToString();
textboxes.Text =
ilist.Tables[0].Rows["PoliciesNum_INT"].ToString();
}
 
M

Munna

Hi

have use another trick to do such kind of things..
I keep all those controls in a dictionary for just keep safe
reference...
Like

Dctionary<string,dropdownlist> mydictionaryddl = new
Dctionary<string,dropdownlist>();
Dctionary<string,textbox> mydictionarytxt = new
Dctionary<string,textbox>();


and add all the dropdown and textbox in their respective
dictionary...
dictionary key can be the integer value of the controls...
after that when access needed

I used

for(int i=0; i <mydictionaryddl .Keys.Count;i++;)
{

Dropdown ddl = ( Dropdown ) mydictionaryddl;
TextBox txt = (Textbox)mydictionarytxt;
//perfrom operation

}

Best of luck

Munna
www.munna.shatkotha.com
www.munna.shatkotha.com/blog
www.shatkotha.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top