I had a situation where I was dynamically creating LinkButtons basically with no properties set on PageInit, and then later on adding the properties about those LinkButtons on another separate button's click event.(ie text, adding a click eventhandler, etc).
The problem was, I had to click the LinkButton twice for the click event handler to fire. Bear in mind, all of this is inside an update panel.
After looking at it up and down, I realized I was setting the ID for it to the same thing twice (on PageInit and also when I later set the properties). I saw that and figured it would muck up things in the control hierarchy and I understand it was the problem...but what I don't fully understand is the why .
Can someone explain to me what the technical cause was for having to click the LinkButton twice and why setting the ID to same thing twice caused this?
CODE
This occurs on CreateChildControls()
Private Sub InitializeLinkBreadCrumbPlaceHolders()
Dim counter As Integer = 0
'Adding the handlers has to take place before/on Page.Init...
For counter = 0 To LEVEL_CAP
_linkDynamic = New LinkButton()
'Add all the links
Me._placeHolder.Controls.Add(_linkDynamic)
With _linkDynamic
AddHandler .Click, AddressOf Link_Click
.Style.Add("display", "none")
.ID = String.Format("lbl{0}", counter)
End With
Next
End Sub
And this occurs when a regular button is pressed (keep in mind all of this is inside an update panel)
Private Sub SetHyperLinkBreadCrumbValues(Optional ByVal ShouldAddAsLink As Boolean = True)
'Don't add a new link if we went backwards
If ShouldAddAsLink Then
Me.Links(Me.CurrentLevel) = Me.LinkHeader
End If
'Go through the collection to set the values of the existing linkbuttons
For Each element As DictionaryEntry In Me.Links
'Links 1-based index
With CType(Me._placeHolder.Controls.Item(CInt(element.Key) - 1), LinkButton)
.Font.Name = "Arial"
.Font.Size = 11
If CInt(element.Key) > 1 Then
.Text = String.Format(" > {0}", CStr(element.Value))
Else
.Text = CStr(element.Value)
End If
.Visible = True
.Style.Add("display", "inline")
End With
Me.TrimDescriptionLink(CType(Me._placeHolder.Controls.Item(CInt(element.Key) - 1), LinkButton))
Next
End Sub
I have seen (and even caused) this behavior from time to time. Inevitably you are not always adding the control (or wiring up the OnClick
event in PageInit
. The first click causes a PostBack
and on the second server page life cycle, you add the control during PageInit
. On that second time around (and second button click), the event is wired up and fires the correct event. Are you possibly not wiring up the OnClick
event every time during PageInit
?
Can you share some code?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.