By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,502 Members | 1,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,502 IT Pros & Developers. It's quick & easy.

Problem executing button-clicks using delegates (with example)

P: n/a
Hi!

This has been annoying me all day, 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

[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]


Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Klaus Jensen" <fo****@bout.it> wrote in message
news:eX**************@TK2MSFTNGP09.phx.gbl...

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

http://www.agilator.dk/temp/delegatetest.zip
Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.