 |
| ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 21st, 2009, 12:41 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Using for loop to add values in text box
Hi All !
I am using multiple text boxes to store values . I want to use a for loop to initialize the values of all text boxes to zero while loading.
the code I am using is as follows :
Code:
for(int i=0;i< rows.length;i++)
{
textboxGrp[i]CP.Text="0";
textboxGrp[i]RP.Text = "0";
textboxGrp[i]TP.Text="0";
}
This is giving me an error.
How to manage this.
Kindly help.
|
|

October 22nd, 2009, 05:46 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
|
|
Can you set the text to 0 in the properties box?
Or are you generating a gridview completely in code?
|
|

October 23rd, 2009, 12:22 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Actually there is no Gridview. I only want t populate a no. of textbox with "0" value.
The name of my TexBox controls are like textboxGrp1, textboxGrp2, textboxGrp3, textboxGrp4........textboxGrp10 . That is why I am trying to use a for loop to pupulate values using the Index of the For loop.
|
|

October 23rd, 2009, 02:57 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
|
|
Can you tell us what error is being displayed?
Check to make sure the you have a tet box called textboxGrp0 and also that the value of rows.length does not exceed the number of text boxes on the page less 1.
But still the easiest way is to set the values to zero in the .aspx page and not on the code behind page. For each Text box (in visual studio) right click, then click properties and set the Text value to 0.
<asp:Textbox ID="textboxGrp1" runat="server">0</asp:TextBox>
|
|

October 25th, 2009, 09:42 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
There is no .aspx page here. It's a Windows Application page. I need to know how to get the index of the textboxes in a for loop. The error is that the control is not there if I am using
for(int i=0;i< rows.length;i++)
{
textboxGrp[i].Text="0";
textboxGrp[i].Text = "0";
textboxGrp[i].Text="0";
}
Is there any solution where I can use the Index of the for loop to assign the values to respective Textboxes. ??? I am trying to use for loop to avoid using hardcoded textboxes names as I have almost 20 textboxes whose names are in a numeric series.
Need urgent Help !!
|
|

October 25th, 2009, 11:35 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
|
|
Almost...
You will have to use the FindControl function....
((Textbox)Form1.FindControl("textboxGrp" + i.ToString)).Text == "0"
__________________
Jason Hall
Follow me on Twitter @jhall2013
|
|

October 26th, 2009, 04:01 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
|
|
.. and don't forget that the series starts with 0
your examples " textboxGrp1, textboxGrp2, textboxGrp3, textboxGrp4" start with 1.
Try populating a text box with the output of your loop rather than processing the values of the text boxes.
You say you have almost 20 boxes, but in your first example you have three separate boxes. so you would need to have either 18 textboxes in 6 rows or 21 textboxes in 7 rows.
Code:
for(int i=0;i< rows.length;i++)
{
textboxGrp[i]CP.Text="0";
textboxGrp[i]RP.Text = "0";
textboxGrp[i]TP.Text="0";
}
In you second example it has changed
Code:
for(int i=0;i< rows.length;i++)
{
textboxGrp[i].Text="0";
textboxGrp[i].Text = "0";
textboxGrp[i].Text="0";
}
Can you show us your full code as I feel we are going round in circles a little.
including the text boxes.
|
|

October 26th, 2009, 04:03 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 107
Thanks: 1
Thanked 8 Times in 7 Posts
|
|
.. and don't forget that the series starts with 0
your examples " textboxGrp1, textboxGrp2, textboxGrp3, textboxGrp4" start with 1.
Try populating a text box with the output of your loop rather than processing the values of the text boxes.
You say you have almost 20 boxes, but in your first example you have three separate boxes. so you would need to have either 18 textboxes in 6 rows or 21 textboxes in 7 rows.
Code:
for(int i=0;i< rows.length;i++)
{
textboxGrp[i]CP.Text="0";
textboxGrp[i]RP.Text = "0";
textboxGrp[i]TP.Text="0";
}
In your second example it has changed
Code:
for(int i=0;i< rows.length;i++)
{
textboxGrp[i].Text="0";
textboxGrp[i].Text = "0";
textboxGrp[i].Text="0";
}
Can you show us your full code as I feel we are going round in circles a little.
including the text boxes.
|
|
 |