Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dynamically Create Linkbutton

Dynamically Create Linkbutton

Scheduled Pinned Locked Moved ASP.NET
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JGOnline
    wrote on last edited by
    #1

    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 below Protected Sub DeleteBtn_Click(ByVal s As Object, ByVal e As CommandEventArgs) Dim MyRecords As String = e.CommandArgument.ToString Response.Write("TheRecordIs" & MyRecords) End Sub

    G M 2 Replies Last reply
    0
    • J JGOnline

      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 below Protected Sub DeleteBtn_Click(ByVal s As Object, ByVal e As CommandEventArgs) Dim MyRecords As String = e.CommandArgument.ToString Response.Write("TheRecordIs" & MyRecords) End Sub

      G Offline
      G Offline
      gnjunge
      wrote on last edited by
      #2

      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.

      J 1 Reply Last reply
      0
      • J JGOnline

        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 below Protected Sub DeleteBtn_Click(ByVal s As Object, ByVal e As CommandEventArgs) Dim MyRecords As String = e.CommandArgument.ToString Response.Write("TheRecordIs" & MyRecords) End Sub

        M Offline
        M Offline
        Mircea Grelus
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • G gnjunge

          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.

          J Offline
          J Offline
          JGOnline
          wrote on last edited by
          #4

          well, you learn something new every day.... Thanks, its working now i have moved the code to load every time the page does. It means extra database hits but the trade off is worth it.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups