Problems with an Array of Controls

W

WesIdell

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
 
O

Otis Mukinfus

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
 
W

WesIdell

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
 
O

Otis Mukinfus

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
 
R

Raven Jones

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.
 
M

Matthew

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
 

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

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top