How to Add Handlers for dynamically created web controls in ASP.net?
-
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>
-
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>
Are you assigning handler to Dynamic Control On Page Load Event (ie- On Every Postback) ??/
-
Are you assigning handler to Dynamic Control On Page Load Event (ie- On Every Postback) ??/
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.
-
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>
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 -
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.
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