addhandler problem

S

Slim

i have a simple page, with one button button1.
when click it creates a new button button 2 and adds a event handler to it.

but when button 2 is clicked nothing happens, why?

Partial Class test_buttons

Inherits System.Web.UI.Page

Dim bt2 As Button

Dim bt3 As Button

Dim test1 As Boolean = False

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

bt2 = New Button

bt2.Text = "Button2"

AddHandler bt2.Click, AddressOf bt2Click

form1.Controls.Add(bt2)

Response.Write("Button 1")

End Sub

Sub bt2Click(ByVal sender As Object, ByVal e As System.EventArgs)

If test1 = True Then

Response.Write("Button 2")

End If

End Sub

End Class
 
K

Karl Seguin

When button2 is clicked, the page postback. At this point button2 doesn't
exist any more. You may think that the ViewState takes care of this, but it
only takes care of maintaing values, not actual controls.

You need to re-create button2 on or before the Load event and hook up it's
event handler to have it work. As a matter of fact, you don't even need to
do the AddHandler in the Button1 click, the AddHandler needs to be done on
postback before/during the load event.

One way people often do this is by storing values in the viewstate,
something like:

Button1_Click(...)
dim bt2 as button = new Button()
...
form1.Controls.Add(bt2)
ViewState.Add("LoadButton2", true)
end sub

OnLoad(...)
if Page.IsPostBack then
if not ViewState("LoadButton2") is nothing AndAlso
cbool(ViewState("LoadButton2")) == true then
dim bt2 as button = new Button()
Addhandler bt2.Click ....
Form1.Controls.Add(bt2)
end if
end if
end sub

Denis Bauer has a special PlaceHolderControl that will do all of this for
you. It's free:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

You should check it out and you might be interested in looking at thee
source code to gain a better fundamental understanding of how it works...

Karl
 
S

Slim

Thanks your example worked fine.
I did try something like this before that made the button persist but did
not add handlers, but all working fine now, thanks allot
 
S

Slim

Actualy i still have a problem

on this page
http://worldgolfdata.com/Enter/Default.aspx
i have a list of countries when you click on them you get a list of states,
click on a state youi get a list of golf courses from that state.

I have a little bit of test data in it, if you go to Australia, then select
either Victoria or Western Australia you will get a list of 1 or 2 courses
repectivly.

I also have a lale that shows what event handler has fired.

if you have a play you will se that when you change state from victoria to
western australia then click on a course the lable shows that the event
handler does not fire till you click the second time.

Can you tell me why and suggets a fix or a workaround?

here is the code



Partial Class Enter_Default
Inherits System.Web.UI.Page

Protected Sub Page_PreLoad(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreLoad
buildCountrys()
If ViewState("buildStates") <> "" Then
buildStates()
End If
If ViewState("buildCourses") <> "" Then
buildCourses()
End If
End Sub

Sub buildCountrys()
Dim oCountries As Countries
oCountries = New Countries
Dim i As Integer
For i = 0 To UBound(oCountries.getCountries)
Dim tr As TableRow = New TableRow
Dim td As TableCell = New TableCell
Dim linkCountry As LinkButton = New LinkButton
AddHandler linkCountry.Click, AddressOf CountryButtClick
linkCountry.OnClientClick = "waitForIt()"
linkCountry.Attributes.Add("class", "dButtons")
linkCountry.CommandArgument = oCountries.getCountries(i).id
linkCountry.Text = oCountries.getCountries(i).Name
td.Controls.Add(linkCountry)
tr.Cells.Add(td)
countryTable.Rows.Add(tr)
Next
End Sub

Sub buildStates()
stateTable.Rows.Clear()
courseTable.Rows.Clear()
Dim oCountry As Country
oCountry = New Country(ViewState("buildStates"))
Dim i As Integer
For i = 0 To UBound(oCountry.getStates)
'On Error Resume Next
Dim tr As TableRow = New TableRow
Dim td As TableCell = New TableCell
Dim linkState As LinkButton = New LinkButton
AddHandler linkState.Click, AddressOf StateButtClick
linkState.OnClientClick = "waitForIt()"
linkState.Attributes.Add("class", "dButtons")
linkState.CommandArgument = oCountry.getStates(i).id
linkState.Text = oCountry.getStates(i).Name
td.Controls.Add(linkState)
tr.Cells.Add(td)
stateTable.Rows.Add(tr)
Next
End Sub

Sub buildCourses()
courseTable.Rows.Clear()
Dim oState As State
oState = New State(ViewState("buildCourses"))
Dim i As Integer
For i = 0 To UBound(oState.getCourses)
If oState.countCourses > 0 Then
Dim tr As TableRow = New TableRow
Dim td As TableCell = New TableCell
Dim linkCourse As LinkButton = New LinkButton
AddHandler linkCourse.Click, AddressOf CourseButtClick
linkCourse.OnClientClick = "waitForIt()"
linkCourse.Attributes.Add("class", "dButtons")
linkCourse.CommandArgument = oState.getCourses(i).id
linkCourse.Text = oState.getCourses(i).Name
td.Controls.Add(linkCourse)
courseTable.Rows.Add(tr)
tr.Cells.Add(td)
Else
End If
Next
End Sub

Sub CountryButtClick(ByVal sender As Object, ByVal e As EventArgs)
ViewState.Add("buildStates", sender.CommandArgument)
Label1.Text = "country clicked"
buildStates()

End Sub

Sub StateButtClick(ByVal sender As Object, ByVal e As EventArgs)
ViewState.Add("buildCourses", sender.CommandArgument)
Label1.Text = "state clicked"
buildCourses()

End Sub

Sub CourseButtClick(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = "course clicked"
End Sub

End Class
 
K

Karl Seguin

Clicking australia doesn't bring anything up for me.

I would look at Denis Bauer's control if you are dealing with that many
dynamic controls.

An alternative would be to forgo the postback mechanism and simply use the
QueryString - this would have the added benefit of being
bookmarkable...sometimes old tricks work best.

Karl
 
S

Slim

Karl Seguin said:
Clicking australia doesn't bring anything up for me.

you may of visited the site whne i was working on it.


I would look at Denis Bauer's control if you are dealing with that many
dynamic controls.

An alternative would be to forgo the postback mechanism and simply use the
QueryString - this would have the added benefit of being
bookmarkable...sometimes old tricks work best.

I would like to work it out al the same

thanks
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top