473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using FOR to edit TextBoxes

Hey all.

I have to check the content of 20 textboxes on my Page_Load event, to
remove the string "nbsp;" from them.
I do it with:
If TextBox1.Text = "nbsp;" Then
TextBox1.Text = ""
End If

Works, but I have a lot of lines on my code... Is there any way to do
a FOR Loop to do this. The Texboxes names are TextBox1 to TextBox20, so
I thought I could use the folllowing command inside the FOR loop:
(Beofre the FOR: Dim i as Integer...)

If TextBox+i = "nbsp;" ' ERROR HERE how to make something like this
work ?

Hoep its possible to do that... thx.

Oct 9 '06 #1
4 1118
You should use FindControl(controlName) to do that.
like FindControl("TextBox" + i).Text = string.Empty;

//Mats

Jl_G_0 wrote:
Hey all.

I have to check the content of 20 textboxes on my Page_Load event, to
remove the string "nbsp;" from them.
I do it with:
If TextBox1.Text = "nbsp;" Then
TextBox1.Text = ""
End If

Works, but I have a lot of lines on my code... Is there any way to do
a FOR Loop to do this. The Texboxes names are TextBox1 to TextBox20, so
I thought I could use the folllowing command inside the FOR loop:
(Beofre the FOR: Dim i as Integer...)

If TextBox+i = "nbsp;" ' ERROR HERE how to make something like this
work ?

Hoep its possible to do that... thx.
Oct 9 '06 #2
It's more efficient to loop over control collections once like that:
--- c# code---
foreach (Control control in this.Controls)
{
TextBox textBox = control as TextBox;
if (textBox != null && textBox.ID.StartsWith("TextBox"))
{
if (textBox.Text == " ")
textBox.Text = String.Empty;
}
}
-- end code ---
Milosz Skalecki
MCP, MCAD
"Ma***@newsgroups.nospam" wrote:
You should use FindControl(controlName) to do that.
like FindControl("TextBox" + i).Text = string.Empty;

//Mats

Jl_G_0 wrote:
Hey all.

I have to check the content of 20 textboxes on my Page_Load event, to
remove the string "nbsp;" from them.
I do it with:
If TextBox1.Text = "nbsp;" Then
TextBox1.Text = ""
End If

Works, but I have a lot of lines on my code... Is there any way to do
a FOR Loop to do this. The Texboxes names are TextBox1 to TextBox20, so
I thought I could use the folllowing command inside the FOR loop:
(Beofre the FOR: Dim i as Integer...)

If TextBox+i = "nbsp;" ' ERROR HERE how to make something like this
work ?

Hoep its possible to do that... thx.
Oct 9 '06 #3
You can go through the entire control hierarchy, but it is recursive, so you
have to check all containers for other controls (panels, etc.).

For each WebControl control in Container.Controls

Start with Container = Page.

You will also have to test each control to make sure it is a textbox, with
GetType().

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
"Jl_G_0" <jl*********@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Hey all.

I have to check the content of 20 textboxes on my Page_Load event, to
remove the string "nbsp;" from them.
I do it with:
If TextBox1.Text = "nbsp;" Then
TextBox1.Text = ""
End If

Works, but I have a lot of lines on my code... Is there any way to do
a FOR Loop to do this. The Texboxes names are TextBox1 to TextBox20, so
I thought I could use the folllowing command inside the FOR loop:
(Beofre the FOR: Dim i as Integer...)

If TextBox+i = "nbsp;" ' ERROR HERE how to make something like this
work ?

Hoep its possible to do that... thx.

Oct 9 '06 #4

Thx all for helping, managed to do it using:

Dim myTbox As TextBox
For i as Integer = 1 to 17
myTbox = FindControl("Text" & i)
If myTbox.Text = "&nbsp;"
myTbox.Text = ""
End If
Next

Thx again. Now I just use 7 lines of code to clean 17 textboxes...

Oct 9 '06 #5

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

Similar topics

8
by: Gilles T. | last post by:
How I can get element ID in the edit mode of datagrid control? If I not in the edit mode, there are no problem. <asp:TemplateColumn ItemStyle-CssClass="grid_column_width_3"...
3
by: Leo | last post by:
I have a datagrid with the first column as a Edit,Update,Cancel button column. The other 5 columns are template columns. When I click the Edit button in IE6 the row correctly displays the...
2
by: Shankar Mani via DotNetMonster.com | last post by:
Hi folks, I am facing a problem while using a DataGrid. I have a webform with a datagrid containing two text boxes for edit viz., countrycode and country name. Like wise I have two more text boxes...
1
by: David Sagenaut | last post by:
Can I set a whole datagrid in edit mode, like a table? I can edit the data in the datagrid. After done, I can click the save button to save the data into a datasousce. I don't want to used the...
1
by: sck10 | last post by:
Hello, I am trying to change a value when a user goes into edit mode on a DetailsView control. I am trying to use the following, but can not figure out how to get to the bound field...
3
by: Slonocode | last post by:
I have some textboxes bound to an access db. I wanted to format the textboxes that displayed currency and date info so I did the following: Dim WithEvents oBidAmt As Binding oBidAmt = New...
7
by: Lorenzino | last post by:
Hi, I have a problem with bindings in a formview. I have a formview; in the insert template i've created a wizard control and inside it i have an HTML table with some textboxes bound to the...
6
by: rn5a | last post by:
When the EditCommandColumn in a DataGrid is clicked, all the BoundColumns get replaced by TextBoxes so that users can alter the data. By default, the Text in the TextBoxes are left-aligned. Is...
3
by: Joey | last post by:
asp.net 2.0 VS2005 C# In my web app I added a gridview and then manually configured boundcolumns, for an in-session dataset to act as a datasource. Then I added an Edit commandfield. When I...
5
by: Kolags | last post by:
Hi all, Please give me guidance to complete this task. As per my requirement, I want to place data in to different textboxes whatever I selected from datagrid using edit command name. Means I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.