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. How to Add Handlers for dynamically created web controls in ASP.net?

How to Add Handlers for dynamically created web controls in ASP.net?

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nethelptutorialquestion
5 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.
  • R Offline
    R Offline
    Rahul Chitte
    wrote on last edited by
    #1

    How to Add Handlers for dynamically created web controls in ASP.net ? I have tried below code but..event is not getting fired..Can anyone help it out..? <pre>For iCounterVar As Integer = 0 To sAnswers.Length - 1                                     Dim t As New TextBox                                     tblCell = New TableCell                                     tblRow = New TableRow                                     t.Text = sAnswers(iCounterVar).ToString                                     AddHandler t.TextChanged, AddressOf MyTextChangedEvent                                     tblCell.Controls.Add(t)                                     tblRow.Controls.Add(tblCell)                                     tblQuestion.Controls.Add(tblRow)                               Next Protected Sub MyTextChangedEvent(ByVal sender As Object, ByVal e As System.EventArgs)             lblMessage = New Label             lblMessage.Text = "Cuaght TextChanged :" & DirectCast(sender, TextBox).Text & " In " ' & DirectCast(sender, TextBox).Parent.ID.ToString)             PlaceHolder1.Controls.Add(lblMessage) End Sub</pre>

    N A 2 Replies Last reply
    0
    • R Rahul Chitte

      How to Add Handlers for dynamically created web controls in ASP.net ? I have tried below code but..event is not getting fired..Can anyone help it out..? <pre>For iCounterVar As Integer = 0 To sAnswers.Length - 1                                     Dim t As New TextBox                                     tblCell = New TableCell                                     tblRow = New TableRow                                     t.Text = sAnswers(iCounterVar).ToString                                     AddHandler t.TextChanged, AddressOf MyTextChangedEvent                                     tblCell.Controls.Add(t)                                     tblRow.Controls.Add(tblCell)                                     tblQuestion.Controls.Add(tblRow)                               Next Protected Sub MyTextChangedEvent(ByVal sender As Object, ByVal e As System.EventArgs)             lblMessage = New Label             lblMessage.Text = "Cuaght TextChanged :" & DirectCast(sender, TextBox).Text & " In " ' & DirectCast(sender, TextBox).Parent.ID.ToString)             PlaceHolder1.Controls.Add(lblMessage) End Sub</pre>

      N Offline
      N Offline
      Nishant Singh
      wrote on last edited by
      #2

      Are you assigning handler to Dynamic Control On Page Load Event (ie- On Every Postback) ??/

      R 1 Reply Last reply
      0
      • N Nishant Singh

        Are you assigning handler to Dynamic Control On Page Load Event (ie- On Every Postback) ??/

        R Offline
        R Offline
        Rahul Chitte
        wrote on last edited by
        #3

        No i am not assigning any handler in page load, in page load i am just calling the method to create these controls, evrything is in the method itself and that code i have alredy pasted. Thanks for your quick reply on this.

        A 1 Reply Last reply
        0
        • R Rahul Chitte

          How to Add Handlers for dynamically created web controls in ASP.net ? I have tried below code but..event is not getting fired..Can anyone help it out..? <pre>For iCounterVar As Integer = 0 To sAnswers.Length - 1                                     Dim t As New TextBox                                     tblCell = New TableCell                                     tblRow = New TableRow                                     t.Text = sAnswers(iCounterVar).ToString                                     AddHandler t.TextChanged, AddressOf MyTextChangedEvent                                     tblCell.Controls.Add(t)                                     tblRow.Controls.Add(tblCell)                                     tblQuestion.Controls.Add(tblRow)                               Next Protected Sub MyTextChangedEvent(ByVal sender As Object, ByVal e As System.EventArgs)             lblMessage = New Label             lblMessage.Text = "Cuaght TextChanged :" & DirectCast(sender, TextBox).Text & " In " ' & DirectCast(sender, TextBox).Parent.ID.ToString)             PlaceHolder1.Controls.Add(lblMessage) End Sub</pre>

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #4

          Read this : http://www.youcanlearnseries.com/programming tips/csharp/SetEvents.aspx As far as I suggest, add eventhandler in Pre_Init than Page_Load:thumbsup:

          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


          My Latest Articles-->** Simplify Code Using NDepend
          Basics of Bing Search API using .NET
          Microsoft Bing MAP using Javascript

          1 Reply Last reply
          0
          • R Rahul Chitte

            No i am not assigning any handler in page load, in page load i am just calling the method to create these controls, evrything is in the method itself and that code i have alredy pasted. Thanks for your quick reply on this.

            A Offline
            A Offline
            Abhishek Sur
            wrote on last edited by
            #5

            If you are calling a method in Page_Load it automatically means you are in Page_Load. ;)

            Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


            My Latest Articles-->** Simplify Code Using NDepend
            Basics of Bing Search API using .NET
            Microsoft Bing MAP using Javascript

            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