Adding buttons with events runtime

K

Klaus Jensen

Hi!

This has been annoying me for a while now, and I can't get it to work It is
really driving me nuts!

Basicly this simple webapp created to illustrate my problem, renders five
buttons, and adds a handler to the click event. When a button is clicked,
the background-color is set to blue.

Try running the page, and clicking the "Do stuff and update controls
again"-button. Now click one of the test-buttons. Notice the click-event
does not fire, the button is not made blue. Click a test-button again, and
notice now the event is firing.

Now try clicking the "Do stuff and do NOT update controls"-button. Then
click the test-buttons. Notice how the events fire nicely!

I am sure this is not a bug, I think it is simply a matter of me not
understanding the flow of the page-events somehow, but I am stuck and would
really appreciate any help.

The demo-app in itself does not make sense, but I think it is the easiest
way to illustrate my problem. In my real-life app, the buttons are rendered
one for each row in an databasetable, and the buttons remove an item from
the databasetable on click. Therefore, when I click a button (testX in the
example) to remove an item, I need to re-render all the buttons, because one
of them has now been removed.

How do I make this work?

Thanks in advance

- Klaus

I have uploaded the Asp.Net project zipped to this url:

http://www.agilator.dk/temp/delegatetest.zip



[aspx]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body>
<form id="Form1" method="post" encType="multipart/form-data"
runat="server">
<P><asp:button id="Button1" runat="server" Text="Do Stuff and Show
Controls again"></asp:button>
<asp:Button id="Button2" runat="server" Text="Do Stuff and do not update
Controls"></asp:Button></P>
<P><asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder></P>
</form>
</body>
</HTML>
[/aspx]

[codebehind, vb]

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ShowMeTheButtons()
End Sub

Sub ShowMeTheButtons()
PlaceHolder1.Controls.Clear()
PlaceHolder1.Controls.Add(MakeFiveTestButtons)
End Sub

Function MakeFiveTestButtons() As PlaceHolder

Dim plc As New PlaceHolder
Dim btnTest As System.Web.UI.WebControls.Button
For i As Integer = 1 To 5
btnTest = New System.Web.UI.WebControls.Button
btnTest.Text = "Test" & i.ToString
btnTest.CommandArgument = i
AddHandler btnTest.Click, AddressOf TestButtonClick
plc.Controls.Add(btnTest)
Next

Return plc

End Function

Private Sub TestButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim btn As System.Web.UI.WebControls.Button = CType(sender,
System.Web.UI.WebControls.Button)
btn.BackColor = System.Drawing.Color.Blue
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Do stuff here which affects the data, the buttons are generated
from in the real-life app
ShowMeTheButtons()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
'Do stuff here which affects the data, the buttons are generated
from in the real-life app
End Sub
[/codebehind, vb]
 

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

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top