How to Raise Event dynamically?

T

TamaHina

I'm trying to create a LinkButton in program code and
raise click event to a function.

Here is the code:
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
Dim temp As LinkButton
temp = New LinkButton
temp.Text = "Click me"
AddHandler temp.Click, AddressOf temp_click
PlaceHolder1.Controls.Add(temp)
End If
End Sub

Private Sub temp_click(ByVal sender As System.Object,_
ByVal e As System.EventArgs)
TextBox1.Text = "You clicked me"
End Sub

However, the program does not go to temp_click.
Why? Anyone can tell me?

Thank you
 
J

John Saunders

TamaHina said:
I'm trying to create a LinkButton in program code and
raise click event to a function.

Here is the code:
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
Dim temp As LinkButton
temp = New LinkButton
temp.Text = "Click me"
AddHandler temp.Click, AddressOf temp_click
PlaceHolder1.Controls.Add(temp)
End If
End Sub

Private Sub temp_click(ByVal sender As System.Object,_
ByVal e As System.EventArgs)
TextBox1.Text = "You clicked me"
End Sub

However, the program does not go to temp_click.
Why? Anyone can tell me?

Assuming you mean that the program doesn't go to temp_click when you click
on the LinkButton, the answer is because you need to add dynamic controls
both on PostBack and non-PostBack requests.
 
T

TamaHina@Notebook

O, I see
Thank you very much

John Saunders said:
Assuming you mean that the program doesn't go to temp_click when you click
on the LinkButton, the answer is because you need to add dynamic controls
both on PostBack and non-PostBack requests.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top