I have a fully dynamiclly generated page that has a set of linkbuttons basically switching views in a UpdatePanel. All that is working great except for when I have a button that is generated that needs to invoke a function to manipulate the data in the Update Panel.
.aspx
<div>
<asp:panel runat="server" ID="buttons" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Code Behind
Private Sub testpage_Init(sender As Object, e As System.EventArgs) Handles Me.Init
For i As Integer = 0 To 5 'create a few dummy buttons
Dim Btn As New LinkButton
Btn.ID = "btn" & i.ToString
Btn.Text = i.ToString
AddHandler Btn.Click, AddressOf btn_Click
buttons.Controls.Add(Btn)
Dim Trgr As New AsyncPostBackTrigger 'async link them to the updatepanel
Trgr.ControlID = Btn.ID
UpdatePanel1.Triggers.Add(Trgr)
Next
End Sub
Private Sub btn_Click(sender As Object, e As System.EventArgs)
ScriptManager.GetCurrent(Me.Page).AddHistoryPoint(sender.id.ToString, sender.id.ToString, sender.id.ToString)
UpdatePanel1.ContentTemplateContainer.Controls.Add(New LiteralControl(sender.id.ToString)) 'Show it got clicked
If sender.id.ToString = "btn1" Then
Dim Btn As New Button
Btn.ID = "test"
Btn.Text = "Click me"
AddHandler Btn.Click, AddressOf subbtn_Click
UpdatePanel1.ContentTemplateContainer.Controls.Add(Btn)
End If
End Sub
Private Sub subbtn_Click(sender As Object, e As System.EventArgs)
UpdatePanel1.ContentTemplateContainer.Controls.Add(New LiteralControl("It doesn't work"))
End Sub
So the problem is when the page_init is executed it does not know which linkbutton was selected so it never recreates the button and in turn never fires the event. I have seen people use the session to store the list of controls and/or the current frame but I don’t see that as an acceptable solution. Surely there has to be a way to do this.
Is there no way I can get the ScriptManager’s current/previous step information outside of the Navigate event?
Asp.net 4.0 with latest ajax toolkit.
Thanks for the help.
This is (I think) one of the fundamental flaws with webforms, and the ways of dealing with it are all a pain. The architecture should have included a native construct to permit event bindings to persist after a postback without recreating a control. Oftentimes you have no interest in using the control after the postback, but you are forced to track & recreate it in order to capture the event.
I usually store a list of strings containing information that can be used to recreate all the controls in
ViewState; then recreate them onLoadViewState; add them to a page as the children of a placeholder control; and remove them from the placeholder again inSaveViewState. This isn’t really that awful, it’s just tedious.Since you’re using an
UpdatePanelyou have the option of using the form field__EVENTTARGETwhich will contain the unique ID of the control that initiated an asynchronous postback. You may be able to extract what you need from that, and take whatever action you need based on that.