Create controls dynamically: Getting an error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all
Pls give me some insight as to what I should be doing.
I have a form where I have 20 text boxes and when an user submits the form I
save it to a database. When they want to retrieve it, I loop and create the
textboxes with ID and text value and then add to the form. The problem is
when trying to create the control in the code behind page I get the error
"Multiple controls with the same ID 'txtQLLink1' were found".
How do I retrieve the data and display it in the text boxes in the form?

Thanks
V
 
Hi Varad,
"Multiple controls with the same ID 'txtQLLink1' were found".
This means that you were not able to assign unique IDs to controls in your
page. Check whether the loop in which you are re-creating the controls, you
are also incrementing the index value for the control. This will ensure that
they get unique names like txtQLLink1, txtQLLink2, txtQLLink3 ...

If this is working fine, then you must be having some other textbox with the
same ID.

HTH,

Need any help, do post a msg back..


Happy Coding
 
Hi Vishnu
Thanks for responding. I have controls already named txtQlLink1, txtQLLink2
etc in the .aspx form. I created those controls by dragging and dropping them
on to the form first. Now, I'm trying to reload the page with values
retrieved from the database but doing it in a loop instead of having to do it
for each control.

This is the code that is used to set the code to the text conrols:
For Each arrValueQL As String In arrQC
qlCount = qlCount + 1
If arrValueQL <> "" Then
If (qlCount Mod 2) = 1 Then
Dim txtQLLink As New TextBox
txtQLLink.ID = "txtQLLink" & i.ToString
txtQLLink.Text = arrValueQL
Me.FindControl("Form1").Controls.Add(txtQLLink)
ElseIf (qlCount Mod 2) = 0 Then
Dim txtQLUrl As New TextBox
txtQLUrl.ID = "txtQLUrl" & i.ToString
txtQLUrl.Text = arrValueQL
Me.FindControl("Form1").Controls.Add(txtQLUrl)
i = i + 1
End If
End If
Next
 
Hi varad!

I have not played with Vb or web development on a .net language yet... but
as far as I can see you allready have a textbox with that id.... I notice
that you are declaring new textboxes where you already have those textboxes
ids in your form. In a regular application you could access the form's
Controls property and itenerate over the allready created control... find
the controls that are textboxes and have the specific id... and just change
their properties, instead of re-declaring a whole new textbox.
I am out of my environment since i code under c# and applications... but I
just thought I would give my two cents.
 
Back
Top