469,927 Members | 1,852 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

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 1355
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Richard Taylor | last post: by
1 post views Thread by hledman | last post: by
15 posts views Thread by Geoff Cox | last post: by
17 posts views Thread by Lloyd Sheen | last post: by
2 posts views Thread by Yogi21 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.