473,472 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problems with an Array of Controls

Hi All:
I need some advice regarding a problem that I'm experiencing. I'm using
a group of TextBox controls in the .aspx page and am using a function
in the .cs code to perform some actions with the values. The problem is
that when I try to retrieve the Text property, it is always equal to ""
(an empty string) even though that a value is keyed in. I'm trying to
do the same function with CheckBoxes and an array as well.
Here's my code...
TextBox[] txBox = { TextBox1, TextBox2, TextBox3, TextBox4, TextBox5,
TextBox6, TextBox7 };

The controls' ids are TextBox1......TextBox7. I don't know if I'm
handling the controls properly in the array or not. Is the way in which
I've written it instanciating a new TextBox1 thus initializing the
values? That is all that I can imagine. Also, if this is the case, what
is the proper way of making an array from like controls so that I can
use a 'for' loop and functions with it instead of writing the code out
the long way. Thanks in advance for any help or advice that anyone can
provide.
Wes

Jan 3 '06 #1
5 1515
On 2 Jan 2006 16:43:10 -0800, We******@gmail.com wrote:
Hi All:
I need some advice regarding a problem that I'm experiencing. I'm using
a group of TextBox controls in the .aspx page and am using a function
in the .cs code to perform some actions with the values. The problem is
that when I try to retrieve the Text property, it is always equal to ""
(an empty string) even though that a value is keyed in. I'm trying to
do the same function with CheckBoxes and an array as well.
Here's my code...
TextBox[] txBox = { TextBox1, TextBox2, TextBox3, TextBox4, TextBox5,
TextBox6, TextBox7 };

The controls' ids are TextBox1......TextBox7. I don't know if I'm
handling the controls properly in the array or not. Is the way in which
I've written it instanciating a new TextBox1 thus initializing the
values? That is all that I can imagine. Also, if this is the case, what
is the proper way of making an array from like controls so that I can
use a 'for' loop and functions with it instead of writing the code out
the long way. Thanks in advance for any help or advice that anyone can
provide.
Wes


Actually there is already a Page.Controls collection already available
to you.

foreach (TextBox tb in Page.Controls)
{
// do your stuff
}

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
Jan 3 '06 #2
Thanks for the information. The problem is that the TextBoxes that I
need to work with is only a subset of all of the TextBoxes on the page.
Is there anyway to differentiate between the two groups using
Page.Controls? I knew that I could get a reference using Page.Controls,
but haven't used this method because of the problems that I mentioned.
Thanks again for the advice.
Wes

Jan 3 '06 #3
On 2 Jan 2006 17:45:59 -0800, We******@gmail.com wrote:
Thanks for the information. The problem is that the TextBoxes that I
need to work with is only a subset of all of the TextBoxes on the page.
Is there anyway to differentiate between the two groups using
Page.Controls? I knew that I could get a reference using Page.Controls,
but haven't used this method because of the problems that I mentioned.
Thanks again for the advice.
Wes


Wes,

Are you dynamically creating the names of the text boxes? If you are
and know the names of them, you can just loop through the controls and
test each one for the names you know are in the list. I'm just
guessing here because I don't know for sure how you create them.
Judging from your first post you do know their names.

Good luck. I Know you will find a way.
Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
Jan 3 '06 #4
Heya Wes,

To answer your second question first, generally, when I want to deal
with a small number of controls on a page where there may be other controls
of the same type present, I'll throw them all into an HtmlGenericControl
just to make them easier to handle:

<span runat="server" id="spnTextboxHolder">
<asp:TextBox runat="server" id="TextBox1" />
<asp:TextBox runat="server" id="TextBox2" />
...
</span>

Then in your codebehind you can say something like

protected HtmlGenericControl spnTextboxHolder;

foreach (TextBox t in this.spnTextboxHolder.Controls)
{
// Playing fast and loose with the type safety, but you get the
picture
...
}

To answer your first question, I suspect that what's happening is that
you're not differentiating the first load of the page from the postback. In
Page_Load() you're probably initializing the values of the textboxes:

this.TextBox1.Text = "";
this.TextBox2.Text = "";
...

but then this code is also getting hit on the postback, and blowing away
the values the user provided before your codebehind can read them. Try

if (! this.IsPostBack)
{
this.TextBox1.Text = "";
this.TextBox2.Text = "";
...
}

and see if that works for you.

Best,
R. Jones.

<We******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks for the information. The problem is that the TextBoxes that I
need to work with is only a subset of all of the TextBoxes on the page.
Is there anyway to differentiate between the two groups using
Page.Controls? I knew that I could get a reference using Page.Controls,
but haven't used this method because of the problems that I mentioned.
Thanks again for the advice.
Wes

Jan 3 '06 #5
Dim thisText as TextBox = Page.FindControl("TextBoxID")
response.write("Textbox says: " & thisText.text)

I recommend making an ArrayList of the id's of your controls
(ArrayLists are serializable) then iterarte the list to "get them"

EX.
Dim myids as New ArrayList
myids.Add("tb1")
myids.Add("tb2")
myids.Add("tb3")
myids.Add("tb4")

Me.ViewState.Add("myIDS", myids)

Then when you get a postback

For Each thisControl as String in Ctype(Me.ViewState("myIDS"),
ArrayList).Array
Dim thisText as TextBox = Page.FindControl(thisControl)
Response.write("Control " & thisControl & " has value " &
thisText.text)
Next

Raven Jones wrote:
Heya Wes,

To answer your second question first, generally, when I want to deal
with a small number of controls on a page where there may be other controls
of the same type present, I'll throw them all into an HtmlGenericControl
just to make them easier to handle:

<span runat="server" id="spnTextboxHolder">
<asp:TextBox runat="server" id="TextBox1" />
<asp:TextBox runat="server" id="TextBox2" />
...
</span>

Then in your codebehind you can say something like

protected HtmlGenericControl spnTextboxHolder;

foreach (TextBox t in this.spnTextboxHolder.Controls)
{
// Playing fast and loose with the type safety, but you get the
picture
...
}

To answer your first question, I suspect that what's happening is that
you're not differentiating the first load of the page from the postback. In
Page_Load() you're probably initializing the values of the textboxes:

this.TextBox1.Text = "";
this.TextBox2.Text = "";
...

but then this code is also getting hit on the postback, and blowing away
the values the user provided before your codebehind can read them. Try

if (! this.IsPostBack)
{
this.TextBox1.Text = "";
this.TextBox2.Text = "";
...
}

and see if that works for you.

Best,
R. Jones.

<We******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks for the information. The problem is that the TextBoxes that I
need to work with is only a subset of all of the TextBoxes on the page.
Is there anyway to differentiate between the two groups using
Page.Controls? I knew that I could get a reference using Page.Controls,
but haven't used this method because of the problems that I mentioned.
Thanks again for the advice.
Wes


Jan 3 '06 #6

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

Similar topics

0
by: Richard Taylor | last post by:
User-Agent: OSXnews 2.07 Xref: number1.nntp.dca.giganews.com comp.lang.python:437315 Hi I am trying to use py2app (http://undefined.org/python/) to package a gnome-python application...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
1
by: hledman | last post by:
Hello, Beginner here reading through murach's c# book and come to a point where the book doesn't give a good example of what they want you to do in the exercise. I've created an array with 5...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
17
by: Lloyd Sheen | last post by:
This IDE is driving me nuts. I needed another button so I copied an existing one, changed the Text and the id and position by drag and drop. Well then I run and get the following: Control...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
2
by: Yogi21 | last post by:
Hi I have a sorted array containing strings. I am iterating through the array and clearing the contents one by one using "array.BinarySearch" to find each element. So far so good. But the moment I...
5
by: Varangian | last post by:
ImageButton ship; ship = new ImageButton; for (int i=0; i<5; i++) { ship.ImageUrl = pathofImage; ship.ID = "ShipNo" + i.ToString(); ship.Click += new...
13
by: Just_a_fan | last post by:
I am adding a bunch of controls with the code below. Problem 1: When program flow passes to "UpperChanged" when I click it, the control name is undefined. When I enter: If udUpperLim1.Value 1...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.