473,383 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How do I loop this

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();
}
Jun 27 '08 #1
3 1167
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

"iHavAQuestion" <iH***********@discussions.microsoft.comwrote in message
news:2A**********************************@microsof t.com...
>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();
}

Jun 27 '08 #2
iHavAQuestion wrote:
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[i].SelectedItem.Text =
ilist.Tables[0].Rows[i]["InsurComp_TEXT"].ToString();
textboxes[i].Text =
ilist.Tables[0].Rows[i]["PoliciesNum_INT"].ToString();
}

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #3
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,dropdownlistmydictionaryddl = new
Dctionary<string,dropdownlist>();
Dctionary<string,textboxmydictionarytxt = 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[i];
TextBox txt = (Textbox)mydictionarytxt[i];
//perfrom operation

}

Best of luck

Munna
www.munna.shatkotha.com
www.munna.shatkotha.com/blog
www.shatkotha.com
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
6
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
11
by: Roman Töngi | last post by:
for (int i = 1; i <= 10; i++) cout << i << endl; I expected the following: 1 2 3 4 5 6
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
3
by: numlock00 | last post by:
I have a nested 'while' loop that won't repeat, no matter how many times the outer loop repeats. The outer loop reads through an array of elements; the inner loop Ithe 'while' loop) is supposed to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.