Puzzling behaviour for dynamically added command buttons....

R

Radu

Hi. I have a page with the following html:

__________________________________________________________
<h1>Dynamic Controls Test</h1>
<hr />
<asp:TextBox ID="txtProduct1" Text="txtProduct 1" runat="server"></
asp:TextBox>
<asp:placeHolder runat="server" id="placeTextBoxes" />
<hr />
<asp:Button ID="cmdPostback" Text="Postback !" runat="server" />
<br />
<asp:label runat="server" id="lblControlList" />
__________________________________________________________

and the code

__________________________________________________________
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Not IsPostBack Then
'Initial load...
ViewState("FieldCount") = 1
End If
DrawAllRows()
End Sub

Protected Sub cmdAddRow_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdAddRow.Click

ViewState("FieldCount") += 1
placeTextBoxes.Controls.Clear()
DrawAllRows()
End Sub

Protected Sub cmdSubtractRow_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdSubtractRow.Click

ViewState("FieldCount") -= 1
placeTextBoxes.Controls.Clear()
DrawAllRows()
End Sub

Protected Sub cmdPostback_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdPostback.Click

lblControlList.Text = ListAllControls(Page)
End Sub

Private Sub DrawAllRows()

Dim intCounter As Integer
Dim intMaxCount As Integer

intMaxCount = CInt(ViewState("FieldCount"))
If intMaxCount > 1 Then
For intCounter = 2 To intMaxCount
AddRowOfControls(intCounter)
Next
End If
lblControlList.Text = ListAllControls(Page)
End Sub

Private Sub AddRowOfControls(ByVal intRowCount As Integer)

Dim litLabel As LiteralControl
Dim txtTextBox As TextBox
Dim cmdAddRow As Button
Dim cmdSubtractRow As Button

litLabel = New LiteralControl
litLabel.Text = "<br>"
placeTextBoxes.Controls.Add(litLabel)

txtTextBox = New TextBox
txtTextBox.Text = "txtProduct " & intRowCount.ToString
placeTextBoxes.Controls.Add(txtTextBox)

cmdAddRow = New Button
cmdAddRow.Text = "+"
AddHandler cmdAddRow.Click, AddressOf cmdAddRow_Click
placeTextBoxes.Controls.Add(cmdAddRow)

cmdSubtractRow = New Button
cmdSubtractRow.Text = "-"
AddHandler cmdSubtractRow.Click, AddressOf cmdSubtractRow_Click
placeTextBoxes.Controls.Add(cmdSubtractRow)
End Sub

Private Function ListAllControls(ByVal objContainter As Control) As
String

Dim strBuilder As New StringBuilder
Dim ctlText As TextBox

For Each ctl As Control In objContainter.Controls
If ctl.Controls.Count > 0 Then
strBuilder.Append(ListAllControls(ctl))
Else
If TypeOf ctl Is TextBox Then
ctlText = CType(ctl, TextBox)
strBuilder.Append("<br>")
strBuilder.Append(ctlText.Text)
End If
End If
Next

If Not ctlText Is Nothing Then
ctlText.Dispose()
End If

Return strBuilder.ToString
End Function
__________________________________________________________

What could be simpler ? And still.... If I click only the + and the -
buttons which have NOT been created dynamically, everything is just
fine. However, if I click on the newly created buttons, let's say on
the + button, I need TWO clicks - one changes the text in the last
textbox from, say, "txtProduct 5" to "txtProduct 2", and the second
click finally adds a new row and now everything is correct - it adds
"txtProduct 6" and I obtain the list of controls as follows:

txtProduct 1
txtProduct 2
txtProduct 3
txtProduct 4
txtProduct 5
txtProduct 6

What's wrong with this approach ? I'm obviously missing something ! ;-
((

Thank you very much.
Alex
 
Y

Yuriy Solodkyy

Hi

That is because ommand buttons clicks are invoked by control ID. When you
recreate controls while handling click event, you create new controls with
new IDs. However, next time on postback the same controls are created with
different IDs. Therefore, if page was generated with recreating controls
command buttons IDs are different.

Try assigning unique IDs to your controls and try not recreating already
existing controls. I.e if you add new line of controls just add it without
clearing existing.

To see your problem replace

strBuilder.Append(ctlText.Text)

with
strBuilder.Append(ctlText.Text + " " + ctlText.UniqueID)

And see how IDs chnage.

yuriy
 
R

Radu

Hi

That is because ommand buttons clicks are invoked by control ID. When you
recreate controls while handling click event, you create new controls with
new IDs. However, next time on postback the same controls are created with
different IDs. Therefore, if page was generated with recreating controls
command buttons IDs are different.

Try assigning unique IDs to your controls and try not recreating already
existing controls. I.e if you add new line of controls just add it without
clearing existing.

To see your problem replace

strBuilder.Append(ctlText.Text)

with
strBuilder.Append(ctlText.Text + " " + ctlText.UniqueID)

And see how IDs chnage.

yuriy















- Show quoted text -


Thank you, thank you, thank you !!!
That was it - it works like a charm now.
Did I say Thank you ? :)))
Alex
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top