-
March 13th, 2013, 02:31 PM
#1
Referencing Controls on ASP.Net page
All,
I am populating a section of an ASP.Net page with the following code:
Code:
if (catList.Count >= 1) {
var CLR = (CategoryLedgerRecord) catList[0];
cboCat1.Text = CLR.CategoryName;
txtAmount1.Text = CLR.Amount.ToString();
}
if (catList.Count >= 2) {
var CLR = (CategoryLedgerRecord) catList[1];
cboCat2.Text = CLR.CategoryName;
txtAmount2.Text = CLR.Amount.ToString();
}
if (catList.Count >= 3) {
var CLR = (CategoryLedgerRecord) catList[2];
cboCat3.Text = CLR.CategoryName;
txtAmount3.Text = CLR.Amount.ToString();
}
if (catList.Count >= 4) {
var CLR = (CategoryLedgerRecord) catList[3];
cboCat4.Text = CLR.CategoryName;
txtAmount4.Text = CLR.Amount.ToString();
}
if (catList.Count >= 5) {
var CLR = (CategoryLedgerRecord) catList[4];
cboCat5.Text = CLR.CategoryName;
txtAmount5.Text = CLR.Amount.ToString();
}
I feel like there must be a simpler way to accomplish this. Since ASP.Net doesn't have control arrays like good 'ol VB6 did I'm having difficulty seeing how I could boil this down to a code fragment that might look like the following:
Code:
for (int i=0; i<5; ++i) {
var CLR = (CategoryLedgerRecord) catList[i];
Controls["cboCat" + (i+1)].Text = CLR.CategoryName;
Controls["txtAmount" + (i+1)].Text = CLR.Amount.ToString();
}
The Controls collection only seems to allow you to reference the controls by index #, no overload for specifying a name (which would be nice). I don't see any methods to allow for this. Anyone know of a way to do this?
This isn't a biggie ... I can manage with the first set of code. I just hate having 5 groups of the same code if I could condense it into a loop!
Thanks,
-Max
If you think it's expensive to hire a professional, wait until you try hiring an amateur! - Red Adair
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|