Page.ParseControl drives me crazy

  • Thread starter Matthias Lohrer
  • Start date
M

Matthias Lohrer

Hi,

I want to add dynamically controls to my aspx-Page by using the
Page.parseControl-method.

Page_Load contains this code:

str = "<asp:TextBox runat='server' id='txtEingabe' TextMode='SingleLine'
/><asp:RequiredFieldValidator id='req' runat='server'
ControlToValidate='txtEingabe'>Bitte um eine
Eingabe</asp:RequiredFieldValidator><asp:button runat='server' id='aspbtn'
text='OK'/>"

parsedCtrl = Page.ParseControl(str)

Dim i, anzahl As Integer

anzahl = parsedCtrl.Controls.Count

myLabel.InnerHtml = "Number of Controls: " & anzahl.ToString & "<br>" '
output is: 3
myLabel.InnerHtml &= "1: " & parsedCtrl.Controls(0).ToString & "<br>" '
System.Web.UI.WebControls.TextBox
myLabel.InnerHtml &= "2: " & parsedCtrl.Controls(1).ToString & "<br>" '
System.Web.UI.WebControls.RequiredFieldValidator
myLabel.InnerHtml &= "3: " & parsedCtrl.Controls(2).ToString & "<br>" '
System.Web.UI.WebControls.Button

myForm.Controls.Add(parsedCtrl.Controls(0)) ' OK
myForm.Controls.Add(parsedCtrl.Controls(1)) ' OK
'myForm.Controls.Add(parsedCtrl.Controls(2)) ' creates an error: index out
of bound !!!???


the problem is this line:
myForm.Controls.Add(parsedCtrl.Controls(2))

it creates the error "index out of bound",

but when I test it with: parsedCtrl.Controls(2).ToString
I get the answer: "System.Web.UI.WebControls.Button"

so the index is valid.

What is going on here?

When I omit this line:
myForm.Controls.Add(parsedCtrl.Controls(2))
I get a form with a inputbox and a button without the validator, which work
well.

I don't understand this. Do you?

Thank you

Matthias


(e-mail address removed)
www.mlohrer.de
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top