hello plzz help me out
how to write the event for a button or anything created at runtime
whoever bothers to help ,thanks to them in advance
hello plzz help me out
how to write the event for a button or anything created at runtime
whoever bothers to help ,thanks to them in advance
Quick summary and what the replies missed: is correct that a runtime control needs a handler attached, and is showing the designer-style Handles syntax (which only works for controls declared at design time). The extra points to know are when to create the control, how to give it a stable ID, and how to pass or recover per-control data so the handler can tell which control fired.
Create the control and attach its event every request (not only on the first load), and do that early in the page lifecycle (OnInit/Page_Init). Give each dynamic control a predictable ID or store identification in CommandArgument/Tag. Use AddHandler to attach a handler at runtime; Handles cannot be used for controls created on the fly. Cast sender inside the handler to read properties (ID, Text, CommandArgument). If events do not fire, the usual cause is that the control was created too late or not recreated on postback.
Example pattern (create in Init, use CommandArgument to identify):
Protected Overrides Sub OnInit(e As EventArgs)
MyBase.OnInit(e)
Dim btn As New Button()
btn.ID = "dyn_1"
btn.Text = "Click"
btn.CommandArgument = "1"
AddHandler btn.Command, AddressOf OnDynamicCommand
placeholder.Controls.Add(btn)
End Sub
Protected Sub OnDynamicCommand(sender As Object, e As CommandEventArgs)
Dim id = e.CommandArgument.ToString()
' handle based on id
End Sub Troubleshooting checklist: recreate controls before Load, keep IDs stable, verify AddHandler runs each request, and prefer a placeholder/container to keep markup organized.
Jump to Post— emurf 2First you would write the procedure that you want to execute when the button is clicked.
Protected sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 'put your code here end subAfter the button is created you use the addhandler to add the event.
First you would write the procedure that you want to execute when the button is clicked.
Protected sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'put your code here
end sub After the button is created you use the addhandler to add the event.
AddHandler button1.clicked, addressof button1_click there are many event for controls. the simple one is clicked. double click on your control (ex button) and the event will shown.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' add code here
End Sub We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.