473,503 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding buttons with events runtime

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]



Nov 19 '05 #1
0 1427

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2989
by: Andrés Giraldo | last post by:
Hi! I'm adding an asp button to a datagrid on the ItemDataBound event, when the user clicks on this button, I basically remove the button and create other 2 buttons... my problem is.. the 2 last...
2
2290
by: manohar.shankar | last post by:
Hi, I have been searching on this topic for quite sometime and didnt get any answer. Is there a way I can extend/add methods/properties to a C# class during runtime. eg., I have class:...
2
1794
by: Doug Slocum | last post by:
Hi, I'm using ASP.NET. I need help adding new buttons to my web page at run time on the server side prior to sending to the client. As the web form is loading, I won't know in advance how many...
1
2015
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like...
6
436
by: Nathan Sokalski | last post by:
I am trying to dynamically add controls to my page, but am having trouble with controls such as buttons. I have been able to add simple controls such as Label controls, because they can be placed...
2
1208
by: Oddball | last post by:
I am trying to create and add a number of ImageButtons to a page when a certain event is called. Not only am I suffering from my page running the "Page_Load" event three times, but I can't seem...
0
2985
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to...
6
1584
by: Totto | last post by:
Hi, Anyone know the best solution to dynamically add buttons to a asp 2.0 page using data from Sql server? Are there any contols suitable for this or is it best to iterate the dataset and...
1
4879
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.