Event handler not firing using AddHandler

眉间皱痕 提交于 2019-12-20 06:11:53

问题


I have a form with about 40 checkboxes. Once a checkbox is checked, the div control's property should be changed from "none" to "block" or vice versa. I don't get an error, but the checkedchanged event isn't handled. Here is the markup:

<tr>
    <td class="sectionSubHeader lightgrey">
        <asp:CheckBox ID="chkbxCOMAEFund" AutoPostBack="true" runat="server" />
        COM Academic Excellence Fund - Endowed
    </td>
</tr>
<tr>
    <td>
        <ul class="boldDetail">
            <li>Financial Need</li>
        </ul>
    </td>
</tr>
<tr>
    <td colspan="2" class="subSectionPad">Description..</td>
</tr>
<tr>
    <td colspan="2" class="subSectionPad">
        <asp:Label ID="lblCOMAEFund" runat="server"></asp:Label><br />
        <div id="divCOMAEFund" runat="server">
            <asp:TextBox ID="txtCOMAEFund" runat="server" TextMode="MultiLine" Columns="95" Rows="4"></asp:TextBox>
        </div>
    </td>
</tr>

Here is the codebehind:

Dim temp As String
Dim div As HtmlControl

For Each ctrl As Control In wizard.WizardSteps
    For Each subCtrl As Control In ctrl.Controls
        If TypeOf (subCtrl) Is CheckBox Then
            temp = subCtrl.ID.Replace("chkbx", "div")
            div = wizard.FindControl(temp)
            div.Style("display") = "none"
            AddHandler CType(subCtrl, CheckBox).CheckedChanged, AddressOf Chkbx_CheckChanged
        End If
    Next
Next

Here is the sub

Private Sub Chkbx_CheckChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim temp As String
    temp = sender.ID
    temp = temp.Replace("chkbx", "div")
    Dim divCtrl As HtmlControl
    divCtrl = wizard1.FindControl(temp)

    If sender.Checked = True Then divCtrl.Style("display") = "block" Else divCtrl.Style("display") = "none"

End Sub

回答1:


The event handlers which you attach in the block of code you've labeled "the codebehind" will only be effective for that iteration of the page. The handlers are not persisted across postbacks. So when the user checks a checkbox and the page is automatically posted back to the server, the event handlers haven't been wired up.

You need to have the event handlers wired up at some stage in the page life cycle before the postback event handling stage. You could do it in PageLoad or declaratively in your markup.

Here's a similar question.



来源:https://stackoverflow.com/questions/3946285/event-handler-not-firing-using-addhandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!