473,414 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Can Someone Please Explain...

Hi

I am trying to test dynamically created controls, to do this i have
added a placeholder to a WbForm and added the following code behind:

Private Property Count() As Integer
Get
If ViewState("Count") Is Nothing Then ViewState("Count") = 1
Return CType(ViewState("Count"), Integer)
End Get
Set(ByVal Value As Integer)
ViewState("Count") = Value
End Set
End Property

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then AddButton()
End Sub

Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
AddButton()
End Sub

Private Sub AddButton()

Dim button As New Button
button.Text = "Button" & Count.ToString
button.ID = "Button" & Count.ToString
AddHandler button.Click, AddressOf button_Click
PlaceHolder1.Controls.Add(button)
Count += 1

End Sub

What i would expect is a button to be added to the placeholder during
the first load of the page, then each time the button is cllicked
another button to be added to the form.

However the original button is added in the load event, but the click
event is never handled on the dynamically created buttons, so on
postbacks no buttons are shown.

Can anyone explain how to acheive what i am afer here?

Thanks

Blu.
Nov 18 '05 #1
4 1355
When you click the button and postback, the button isn't re-created, thus
the event is never fired. People make this mistake often. You don't have
to set the eventhandler when originally creating the item, you need to set
it on postback.

If you simply remove the not ispostback check and always call AddButton in
page_load it'll work.

To reinforce my original point though, you could actually do:

Dim button As New Button
button.Text = "Button" & Count.ToString
button.ID = "Button" & Count.ToString
if page.ispostback then
AddHandler button.Click, AddressOf button_Click
end if
PlaceHolder1.Controls.Add(button)
Count += 1

notice how the handler is only hooked up on postback - I don't recommend it,
since it'll only avoid a little overhead but will make it look more
confusing. I just wanted to point it out to illustrate that events need to
be hooked on postback, not on non-postback.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"BluDog" <ne**@nospam.bludog.net> wrote in message
news:uh********************************@4ax.com...
Hi

I am trying to test dynamically created controls, to do this i have
added a placeholder to a WbForm and added the following code behind:

Private Property Count() As Integer
Get
If ViewState("Count") Is Nothing Then ViewState("Count") = 1
Return CType(ViewState("Count"), Integer)
End Get
Set(ByVal Value As Integer)
ViewState("Count") = Value
End Set
End Property

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then AddButton()
End Sub

Private Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
AddButton()
End Sub

Private Sub AddButton()

Dim button As New Button
button.Text = "Button" & Count.ToString
button.ID = "Button" & Count.ToString
AddHandler button.Click, AddressOf button_Click
PlaceHolder1.Controls.Add(button)
Count += 1

End Sub

What i would expect is a button to be added to the placeholder during
the first load of the page, then each time the button is cllicked
another button to be added to the form.

However the original button is added in the load event, but the click
event is never handled on the dynamically created buttons, so on
postbacks no buttons are shown.

Can anyone explain how to acheive what i am afer here?

Thanks

Blu.

Nov 18 '05 #2
>If you simply remove the not ispostback check and always call AddButton in
page_load it'll work.


Karl,

Thanks for your comments, however this only sorts out the event
handler for the original button (button1) added in the page load. Any
buttons added in the click event of the button are not added.

This is only a test page that i knocked up to try and understand
ViewState and PostBack on dynamically created controls. As it stands i
can't see events on dynamically created controls evern being handled
properly as more and more postbacks take place.

Cheeers

Blu.
Nov 18 '05 #3
Blu,
I understand where you are coming from. However the same holds true with
the buttons added in the click...they also need to be readded.

Denis Bauer has a placeholder control that helps you do this..though I've
never used it:
http://www.denisbauer.com/ASPNETCont...aceholder.aspx
Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"BluDog" <ne**@nospam.bludog.net> wrote in message
news:88********************************@4ax.com...
If you simply remove the not ispostback check and always call AddButton inpage_load it'll work.


Karl,

Thanks for your comments, however this only sorts out the event
handler for the original button (button1) added in the page load. Any
buttons added in the click event of the button are not added.

This is only a test page that i knocked up to try and understand
ViewState and PostBack on dynamically created controls. As it stands i
can't see events on dynamically created controls evern being handled
properly as more and more postbacks take place.

Cheeers

Blu.

Nov 18 '05 #4
On Mon, 4 Oct 2004 14:17:11 -0400, "Karl Seguin" <karl REMOVE @ REMOVE
openmymind REMOVEMETOO . ANDME net> wrote:
Blu,
I understand where you are coming from. However the same holds true with
the buttons added in the click...they also need to be readded.

Denis Bauer has a placeholder control that helps you do this..though I've
never used it:
http://www.denisbauer.com/ASPNETCont...aceholder.aspx
Karl


Thanks Karl, you have pretty much confirmed my conclusions on this -
every postback needs to be aware of previous postbacks to ensure
anything done dynamically previously can be repeated. Thus making an
application in a webpage a little difficult.

Interesting control though, gunna have a good play today.
Nov 18 '05 #5

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

Similar topics

8
by: Igor Raytsin | last post by:
Hello All, The following script is reproducing the problem assuming you have Northwind database on the server. Please note it gives you the error message on line 12. USE tempdb GO...
5
by: klj_mcsd | last post by:
Let's say you set txtlastname1.visible = true Then DirectCast(Page.FindControl("txtLastName1"), TextBox).Visible = False Why when you check txtlastname1.visible it is equal to True?
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
1
by: Simon Windsor | last post by:
Hi I have just recevived this error could not write to hash-join temporary file: No space left on device Can someone please explain how I can stop this occuring. Whereis the hash-join...
5
by: garyusenet | last post by:
I understand that Point is an object used for storing co-ordinates, such as mouse location. I can't figure out what the e's do in the above example can someone explain please. Thanks, Gary.
2
by: hassruby | last post by:
Can someone pls help me with some validation that im having a few technical problems with in my program. First of all, I will explain to you a little about what my program is suppose to do. It...
4
by: fatboySudsy | last post by:
Hi, I have constructed a client program that has given me some error codes that i just cannot see. I was wondering if a different set of eyes with much more experience than me could help me out. ...
1
by: td0g03 | last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be...
3
by: Aarti | last post by:
Hi, Can some one please explain why the output of this program is 15 #include <iostream> using namespace std; class A {
1
by: tangierc | last post by:
Can someone please take a look at my test site and tell me why I can't get reflect.js to work. I've implemented it per the instructions and my directory calls are correct. I can't figure this one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.