Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a ControlCollection

Status
Not open for further replies.

vilrbn

Programmer
Oct 29, 2002
105
FR
Hello,

I have a lot of textboxes on a Form and want to create a ControlCollection.

Here is my code:

Protected WithEvents textBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents textBox2 As System.Web.UI.WebControls.TextBox
Protected WithEvents txtCol As System.Web.UI.Control
Protected WithEvents revtxtBox As System.Web.UI.WebControls.RegularExpressionValidator


Dim txtCol As New System.Web.UI.Control()
Dim ctl As New ControlCollection(txtCol)
Dim revtxtBox As New System.Web.UI.WebControls.RegularExpressionValidator()

ctl.Add(textBox1)
ctl.Add(textBox2)

For Each txtCol In ctl
revtxtBox.ControlToValidate = txtCol.ID
revtxtBox.ValidationExpression = "\w{4}"
revtxtBox.ErrorMessage = "test"
Next


I got no error message but TextBox1 and TextBox2 disappear from my Form.
Can anybody help me to find why ?
Many thanks.
 
Hi
I don't know where this code comes from - does some of it come from the web designer generated region?

But anyway, you are setting up the textBox1 and 2 variables to hold text box objects, but in the code, they are never initiated (ie with the new keyword)

The opposite is the case with the txtCol temporary variable. Here, there is no need to declare this variable as new, as you have already declared it at the top, and it is just a temporary variable for the for each loop.

I am surprised that you are not getting an error when you try to do

ctl.Add(textBox1), as it does not seem that textBox1 points to anything.

I am guessing these variables are being initiated in the web forms designer region.

Give this a try


Remove the line
Protected WithEvents txtCol As System.Web.UI.Control

And replace the line

Dim txtCol As New System.Web.UI.Control()

with

Dim txtCol As System.Web.UI.Control()

And try it again

Mark [openup]
 
Oups, oups, code comes from me...
If I follow your instructions, I get the following message:

Value cannot be null. Parameter name: owner

once ctl is initiated.
Concerning TextBox1 and 2, there are ASP textboxes, so no need to initiate them.

<asp:textbox id=&quot;textBox1&quot; runat=&quot;server&quot; AutoPostBack=&quot;True&quot; Enabled=&quot;False&quot;></asp:textbox><br>

And if I remove the line:

Protected WithEvents txtCol As System.Web.UI.Control

How can I fire an event within my collection ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top