Dynamically Create Linkbutton
-
I am dynamically creating a load of link buttons and adding them to the page as below
MyLB = New LinkButton MyLB.ID = "ObliLinkButton" & MyReader("ObligationID") MyLB.CommandArgument = MyReader("ObligationID") MyLB.CommandName = "DeleteMe" AddHandler MyLB.Command, AddressOf Me.DeleteBtn_Click MyLB.Text = "Delete" Obligationslbl.Controls.Add(MyLB)
The buttons all render as they should but when i click them they dont fire the code belowProtected Sub DeleteBtn_Click(ByVal s As Object, ByVal e As CommandEventArgs) Dim MyRecords As String = e.CommandArgument.ToString Response.Write("TheRecordIs" & MyRecords) End Sub
-
I am dynamically creating a load of link buttons and adding them to the page as below
MyLB = New LinkButton MyLB.ID = "ObliLinkButton" & MyReader("ObligationID") MyLB.CommandArgument = MyReader("ObligationID") MyLB.CommandName = "DeleteMe" AddHandler MyLB.Command, AddressOf Me.DeleteBtn_Click MyLB.Text = "Delete" Obligationslbl.Controls.Add(MyLB)
The buttons all render as they should but when i click them they dont fire the code belowProtected Sub DeleteBtn_Click(ByVal s As Object, ByVal e As CommandEventArgs) Dim MyRecords As String = e.CommandArgument.ToString Response.Write("TheRecordIs" & MyRecords) End Sub
You should rebuild the items you have build dynamically after post back. Because of the stateless nature the page does not know that in the last cycle you created a button with an eventhandler. In the postback data there is written that button X fired an event, but since button X does not exist anymore after the postback (you did not rebuild it) it cannot handle that event.
-
I am dynamically creating a load of link buttons and adding them to the page as below
MyLB = New LinkButton MyLB.ID = "ObliLinkButton" & MyReader("ObligationID") MyLB.CommandArgument = MyReader("ObligationID") MyLB.CommandName = "DeleteMe" AddHandler MyLB.Command, AddressOf Me.DeleteBtn_Click MyLB.Text = "Delete" Obligationslbl.Controls.Add(MyLB)
The buttons all render as they should but when i click them they dont fire the code belowProtected Sub DeleteBtn_Click(ByVal s As Object, ByVal e As CommandEventArgs) Dim MyRecords As String = e.CommandArgument.ToString Response.Write("TheRecordIs" & MyRecords) End Sub
The thing is that the postback resets the page to it's original state, so any information regarding the dynamically created controls is lost. Why is that? Because the
Page
class is stateless. The Page recreates child controls based on the tags in the aspx files, so your controls not being present there are not shown. What you need to do is recreate the controls in the OnInit event or any event that fires before PageLoad, or in PageLoad if you do a check to see is the page is posted back. See this article[^] as an example.regards, Mircea Many people spend their life going to sleep when they’re not sleepy and waking up while they still are.
-
You should rebuild the items you have build dynamically after post back. Because of the stateless nature the page does not know that in the last cycle you created a button with an eventhandler. In the postback data there is written that button X fired an event, but since button X does not exist anymore after the postback (you did not rebuild it) it cannot handle that event.