473,397 Members | 2,028 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,397 software developers and data experts.

Problem using AddHandler for dynamically created WebControls

I am using the AddHandler statement to add a CheckedChanged event handler to
a series of RadioButtons that I create in a loop. However, the handler is
not being called for a reason I cannot determine. What is the problem? Here
is my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
currpoem = Server.UrlDecode(Request.QueryString("poem"))
Dim ratinglabels As New TableRow
Dim ratingselection As New TableRow
For i As Integer = 1 To 10
Dim currlabel As New TableCell
currlabel.EnableViewState = False
currlabel.Font.Names = New String() {"Arial", "Helvetica",
"Sans-Serif"}
currlabel.Font.Size = FontUnit.Medium
currlabel.HorizontalAlign = HorizontalAlign.Center
currlabel.Text = i
ratinglabels.Cells.Add(currlabel)

Dim currselection As New TableCell
Dim radioselection As New RadioButton
currselection.EnableViewState = False
currselection.HorizontalAlign = HorizontalAlign.Center
radioselection.GroupName = "poemscore"
radioselection.ID = "radScore" & i
AddHandler radioselection.CheckedChanged, AddressOf
Me.Rating_CheckedChanged
currselection.Controls.Add(radioselection)
ratingselection.Cells.Add(currselection)
Next
tblRatingChoices.Rows.Add(ratinglabels)
tblRatingChoices.Rows.Add(ratingselection)
lblRatePoem.Text = "Rate Poem:<br>" & currpoem
TitleBar.InnerText = "Rate Poem: " & currpoem
btnClose.Attributes.Add("onClick=", "window.close();")
End Sub

Private Sub Rating_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
score = CByte(CType(sender, RadioButton).ID.Substring(8))
btnSubmit.Enabled = True
End Sub
When the page is run, everything looks as expected, but the CheckedChanged
event does not fire (or at least it does not call the Rating_CheckedChanged
procedure as expected). Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
3 1649
Nathan, the checked_changed event needs a "handles" clause. This is added by
default, but it will magically disappear if you, say, go into the html and
change the id of the control. Add your own "handles..." or start over.

Bill

"Nathan Sokalski" wrote:
I am using the AddHandler statement to add a CheckedChanged event handler to
a series of RadioButtons that I create in a loop. However, the handler is
not being called for a reason I cannot determine. What is the problem? Here
is my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
currpoem = Server.UrlDecode(Request.QueryString("poem"))
Dim ratinglabels As New TableRow
Dim ratingselection As New TableRow
For i As Integer = 1 To 10
Dim currlabel As New TableCell
currlabel.EnableViewState = False
currlabel.Font.Names = New String() {"Arial", "Helvetica",
"Sans-Serif"}
currlabel.Font.Size = FontUnit.Medium
currlabel.HorizontalAlign = HorizontalAlign.Center
currlabel.Text = i
ratinglabels.Cells.Add(currlabel)

Dim currselection As New TableCell
Dim radioselection As New RadioButton
currselection.EnableViewState = False
currselection.HorizontalAlign = HorizontalAlign.Center
radioselection.GroupName = "poemscore"
radioselection.ID = "radScore" & i
AddHandler radioselection.CheckedChanged, AddressOf
Me.Rating_CheckedChanged
currselection.Controls.Add(radioselection)
ratingselection.Cells.Add(currselection)
Next
tblRatingChoices.Rows.Add(ratinglabels)
tblRatingChoices.Rows.Add(ratingselection)
lblRatePoem.Text = "Rate Poem:<br>" & currpoem
TitleBar.InnerText = "Rate Poem: " & currpoem
btnClose.Attributes.Add("onClick=", "window.close();")
End Sub

Private Sub Rating_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
score = CByte(CType(sender, RadioButton).ID.Substring(8))
btnSubmit.Enabled = True
End Sub
When the page is run, everything looks as expected, but the CheckedChanged
event does not fire (or at least it does not call the Rating_CheckedChanged
procedure as expected). Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 19 '05 #2

"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:uW*************@TK2MSFTNGP09.phx.gbl...
I am using the AddHandler statement to add a CheckedChanged event handler
to a series of RadioButtons that I create in a loop. However, the handler
is not being called for a reason I cannot determine. What is the problem?
Here is my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
currpoem = Server.UrlDecode(Request.QueryString("poem"))
Dim ratinglabels As New TableRow
Dim ratingselection As New TableRow
For i As Integer = 1 To 10
Dim currlabel As New TableCell
currlabel.EnableViewState = False
currlabel.Font.Names = New String() {"Arial", "Helvetica",
"Sans-Serif"}
currlabel.Font.Size = FontUnit.Medium
currlabel.HorizontalAlign = HorizontalAlign.Center
currlabel.Text = i
ratinglabels.Cells.Add(currlabel)

Dim currselection As New TableCell
Dim radioselection As New RadioButton
currselection.EnableViewState = False
currselection.HorizontalAlign = HorizontalAlign.Center
radioselection.GroupName = "poemscore"
radioselection.ID = "radScore" & i
AddHandler radioselection.CheckedChanged, AddressOf
Me.Rating_CheckedChanged
currselection.Controls.Add(radioselection)
ratingselection.Cells.Add(currselection)
Next
tblRatingChoices.Rows.Add(ratinglabels)
tblRatingChoices.Rows.Add(ratingselection)
lblRatePoem.Text = "Rate Poem:<br>" & currpoem
TitleBar.InnerText = "Rate Poem: " & currpoem
btnClose.Attributes.Add("onClick=", "window.close();")
End Sub

Private Sub Rating_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
score = CByte(CType(sender, RadioButton).ID.Substring(8))
btnSubmit.Enabled = True
End Sub
When the page is run, everything looks as expected, but the CheckedChanged
event does not fire (or at least it does not call the
Rating_CheckedChanged procedure as expected). Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


This is because you are creating the control in Page_Load, and then
attaching the event handler to it. During the next postback, the control
will get created again during page load and have the event handler attached
to it. But the code that actually sync's the event to the event handler has
already occured (before Page_Load is called)...therefore, you are expecting
an event to be raised even though there is no control even created for the
handler.

So, try placing the code you have in Page_Load to Initialize (or whatever
that darn method's name is). See if that works for ya :)

Mythran

Nov 19 '05 #3
That was my first idea when writing the page (to manually create each of the
RadioButtons rather than generate them), so I decided to do that. Here is
what I have now for the event handler:

Private Sub Rating_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles radSelection1.CheckedChanged,
radSelection2.CheckedChanged, radSelection3.CheckedChanged,
radSelection4.CheckedChanged, radSelection5.CheckedChanged,
radSelection6.CheckedChanged, radSelection7.CheckedChanged,
radSelection8.CheckedChanged, radSelection9.CheckedChanged,
radSelection10.CheckedChanged

btnSubmit.Enabled = True

score = CByte(CType(sender, RadioButton).ID.Substring(12))

End Sub
As you can see, Rating_CheckedChanged is (supposedly) called by 10 different
RadioButton's CheckedChanged events. However, when I ran the debugger this
process was never called. I would think it would be called whenever the user
selected a different RadioButton (since radSelection1-radSelection10 all
have the same GroupName). Since the RadioButtons and Handles clause are
always there in my code (they are not dynamically added anymore), what could
be the problem? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Bill Borg" <Bi******@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
Nathan, the checked_changed event needs a "handles" clause. This is added
by
default, but it will magically disappear if you, say, go into the html and
change the id of the control. Add your own "handles..." or start over.

Bill

"Nathan Sokalski" wrote:
I am using the AddHandler statement to add a CheckedChanged event handler
to
a series of RadioButtons that I create in a loop. However, the handler is
not being called for a reason I cannot determine. What is the problem?
Here
is my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
currpoem = Server.UrlDecode(Request.QueryString("poem"))
Dim ratinglabels As New TableRow
Dim ratingselection As New TableRow
For i As Integer = 1 To 10
Dim currlabel As New TableCell
currlabel.EnableViewState = False
currlabel.Font.Names = New String() {"Arial",
"Helvetica",
"Sans-Serif"}
currlabel.Font.Size = FontUnit.Medium
currlabel.HorizontalAlign = HorizontalAlign.Center
currlabel.Text = i
ratinglabels.Cells.Add(currlabel)

Dim currselection As New TableCell
Dim radioselection As New RadioButton
currselection.EnableViewState = False
currselection.HorizontalAlign = HorizontalAlign.Center
radioselection.GroupName = "poemscore"
radioselection.ID = "radScore" & i
AddHandler radioselection.CheckedChanged, AddressOf
Me.Rating_CheckedChanged
currselection.Controls.Add(radioselection)
ratingselection.Cells.Add(currselection)
Next
tblRatingChoices.Rows.Add(ratinglabels)
tblRatingChoices.Rows.Add(ratingselection)
lblRatePoem.Text = "Rate Poem:<br>" & currpoem
TitleBar.InnerText = "Rate Poem: " & currpoem
btnClose.Attributes.Add("onClick=", "window.close();")
End Sub

Private Sub Rating_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
score = CByte(CType(sender, RadioButton).ID.Substring(8))
btnSubmit.Enabled = True
End Sub
When the page is run, everything looks as expected, but the
CheckedChanged
event does not fire (or at least it does not call the
Rating_CheckedChanged
procedure as expected). Any ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 19 '05 #4

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

Similar topics

1
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say...
4
by: The Alchemist | last post by:
I am having a problem with a dynamically-generated Datagrid. It is important to point out that this problem does not exist with a design-time created Datagrid, but only with a dynamically generated...
1
by: Klaus Jensen | last post by:
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...
4
by: Girish | last post by:
Im trying to create a grid within a grid programmatically. Ive been successful in doing this but I need the embedded grid to fire its ItemDataBound event so I can handle it. The event does not seem...
3
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
4
by: DJ | last post by:
Good morning, Still new at this so please bear with me. I am creating a table dynamically using webcontrols based on the output of a sproc from my database.The table represents test instances...
2
by: Kjell Kristiansson | last post by:
I have not been able to figure out how to catch the click event from a button that is added dynamically to a table. In priciple the process is as follows: When I first create the page I analyse...
3
by: Nathan Sokalski | last post by:
I am using the AddHandler statement to add a CheckedChanged event handler to a series of RadioButtons that I create in a loop. However, the handler is not being called for a reason I cannot...
3
by: =?Utf-8?B?UnVkeQ==?= | last post by:
Hello All! I may have posted this problem before, but it was awhile ago. I'm working in VS 2003. I have 10 file watch process going. It seems that I can only about 6or 7 to run at the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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...
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
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
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
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...

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.