Problem using AddHandler for dynamically created WebControls

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default 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
    [email]njsokalski@hotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]


    Nathan Sokalski Guest

  2. Similar Questions and Discussions

    1. Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created)
      Ok here's my scenario. I have a Custom Composite Server Control (CCSC) consisting of a TextBox, Button & Panel. (And some other code - which I...
    2. applying xsl to dynamically created xml
      I've been puzzling over this for hours. I hope someone here can help me out. I'm building a site that takes data from a MYSQL database and then...
    3. Dynamically created control in Datagrid problem
      I created a datagrid with 4 bound columns and 1 template column. I am using the ITEMDATABOUND event to add a control to the template column, if...
    4. Access dynamically created controls
      Hi there, I read a lot about this issue but still got no clear answer that solves my problem. I've a Web User Control with a placeholder called...
    5. get formvalues - dynamically created controls
      C.H., You could loop through them using their index. Instead of using FindControl and the control's name you could just use the index of the...
  3. #2

    Default RE: Problem using AddHandler for dynamically created WebControls

    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
    > [email]njsokalski@hotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >
    >
    >
    Bill Borg Guest

  4. #3

    Default Re: Problem using AddHandler for dynamically created WebControls


    "Nathan Sokalski" <njsokalski@hotmail.com> wrote in message
    news:uWYa66GbFHA.796@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
    > [email]njsokalski@hotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >
    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

    Mythran Guest

  5. #4

    Default Re: Problem using AddHandler for dynamically created WebControls

    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
    [email]njsokalski@hotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]

    "Bill Borg" <BillBorg@discussions.microsoft.com> wrote in message
    news:588E3111-3EBB-44F1-941A-88A469CA953C@microsoft.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
    >> [email]njsokalski@hotmail.com[/email]
    >> [url]http://www.nathansokalski.com/[/url]
    >>
    >>
    >>

    Nathan Sokalski Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139