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.P age
Protected Sub Page_PreLoad(By Val sender As Object, ByVal e As
System.EventArg s) Handles Me.PreLoad
buildCountrys()
If ViewState("buil dStates") <"" Then
buildStates()
End If
If ViewState("buil dCourses") <"" Then
buildCourses()
End If
End Sub
Sub buildCountrys()
Dim oCountries As Countries
oCountries = New Countries
Dim i As Integer
For i = 0 To UBound(oCountri es.getCountries )
Dim tr As TableRow = New TableRow
Dim td As TableCell = New TableCell
Dim linkCountry As LinkButton = New LinkButton
AddHandler linkCountry.Cli ck, AddressOf CountryButtClic k
linkCountry.OnC lientClick = "waitForIt( )"
linkCountry.Att ributes.Add("cl ass", "dButtons")
linkCountry.Com mandArgument = oCountries.getC ountries(i).id
linkCountry.Tex t = oCountries.getC ountries(i).Nam e
td.Controls.Add (linkCountry)
tr.Cells.Add(td )
countryTable.Ro ws.Add(tr)
Next
End Sub
Sub buildStates()
stateTable.Rows .Clear()
courseTable.Row s.Clear()
Dim oCountry As Country
oCountry = New Country(ViewSta te("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.OnCli entClick = "waitForIt( )"
linkState.Attri butes.Add("clas s", "dButtons")
linkState.Comma ndArgument = oCountry.getSta tes(i).id
linkState.Text = oCountry.getSta tes(i).Name
td.Controls.Add (linkState)
tr.Cells.Add(td )
stateTable.Rows .Add(tr)
Next
End Sub
Sub buildCourses()
courseTable.Row s.Clear()
Dim oState As State
oState = New State(ViewState ("buildCourses" ))
Dim i As Integer
For i = 0 To UBound(oState.g etCourses)
If oState.countCou rses 0 Then
Dim tr As TableRow = New TableRow
Dim td As TableCell = New TableCell
Dim linkCourse As LinkButton = New LinkButton
AddHandler linkCourse.Clic k, AddressOf CourseButtClick
linkCourse.OnCl ientClick = "waitForIt( )"
linkCourse.Attr ibutes.Add("cla ss", "dButtons")
linkCourse.Comm andArgument = oState.getCours es(i).id
linkCourse.Text = oState.getCours es(i).Name
td.Controls.Add (linkCourse)
courseTable.Row s.Add(tr)
tr.Cells.Add(td )
Else
End If
Next
End Sub
Sub CountryButtClic k(ByVal sender As Object, ByVal e As EventArgs)
ViewState.Add(" buildStates", sender.CommandA rgument)
Label1.Text = "country clicked"
buildStates()
End Sub
Sub StateButtClick( ByVal sender As Object, ByVal e As EventArgs)
ViewState.Add(" buildCourses", sender.CommandA rgument)
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
"Slim" <me@here.comwro te in message
news:O5******** ******@TK2MSFTN GP04.phx.gbl...
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
"Karl Seguin" <ka********@rem oveopenmymindre movemetoo.andme netwrote in
message news:OC******** *****@TK2MSFTNG P04.phx.gbl...
>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("Load Button2") is nothing AndAlso
cbool(ViewStat e("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 PlaceHolderCont rol that will do all of this for
you. It's free:
http://www.denisbauer.com/ASPNETCont...aceholder.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
--
http://www.openmymind.net/
http://www.codebetter.com/
"Slim" <me@here.comwro te in message
news:%2******* ********@TK2MSF TNGP02.phx.gbl. ..
>>>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.P age
Dim bt2 As Button
Dim bt3 As Button
Dim test1 As Boolean = False
Protected Sub Button1_Click(B yVal sender As Object, ByVal e As
System.EventA rgs) 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.EventArg s)
If test1 = True Then
Response.Write( "Button 2")
End If
End Sub
End Class