473,804 Members | 4,269 Online
Bytes | Software Development & Data Engineering Community
+ 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......T extBox7. 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 1540
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......T extBox7. 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.Control s? 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 HtmlGenericCont rol
just to make them easier to handle:

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

Then in your codebehind you can say something like

protected HtmlGenericCont rol spnTextboxHolde r;

foreach (TextBox t in this.spnTextbox Holder.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.T ext = "";
this.TextBox2.T ext = "";
...

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.T ext = "";
this.TextBox2.T ext = "";
...
}

and see if that works for you.

Best,
R. Jones.

<We******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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.FindContro l("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.Ad d("myIDS", myids)

Then when you get a postback

For Each thisControl as String in Ctype(Me.ViewSt ate("myIDS"),
ArrayList).Arra y
Dim thisText as TextBox = Page.FindContro l(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 HtmlGenericCont rol
just to make them easier to handle:

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

Then in your codebehind you can say something like

protected HtmlGenericCont rol spnTextboxHolde r;

foreach (TextBox t in this.spnTextbox Holder.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.T ext = "";
this.TextBox2.T ext = "";
...

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.T ext = "";
this.TextBox2.T ext = "";
...
}

and see if that works for you.

Best,
R. Jones.

<We******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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
2026
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 called gramps (http://www.gramps-project.org) for MAC OS X.
10
2417
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) ComboBox.SelectedValue = db_value; If the db_value was not included in the ComboBox value list the ComboBox.SelectedIndex used to return -1, Now the very same code is
1
2045
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 elements that all start with their default value 0. I need to replace those 0's with values i enter into the application. When trying to solve this problem i've run into two outcomes. First is when I enter a value and click calcualte it stores...
15
5331
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 private string myArray;
17
2507
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 'Button19' of type 'Button' must be placed inside a form tag with runat=server Can the IDE not do what it is supposed to do. It seems that it is a fight to make it do anything or did I do something wrong? It would seem silly to have to create a...
2
3186
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 problems that I have encountered to date and the solutions (if any) that I found. http://users.adelphia.net/~brianpclab/ServerControlCollectionIssues.htm This page also has all of the source code in a compressed file that you are free to download...
2
1510
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 come to the last element, the BinarySearch method fails.It gives me a return value which is negative although the element is very much there. I am debugging through the code and I find nothing wrong with it.Note that i am using the first overloaded...
5
6468
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 ImageClickEventHandler(this.ImageBtn_Click); this.Form1.Controls.Add(ship); }
13
1707
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 Then I get an error that udUpperLim1 is "Not Defined" so I cannot get the value in the control.
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4297
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 we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.