使用 AddHandler 未触发事件处理程序

发布于 2024-09-28 08:48:15 字数 1875 浏览 3 评论 0原文

我有一个包含大约 40 个复选框的表单。选中复选框后,div 控件的属性应从“none”更改为“block”,反之亦然。我没有收到错误,但未处理 checkchanged 事件。这是标记:

<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>

这是隐藏代码:

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

这是子代码

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏九 2024-10-05 08:48:15

您附加在标记为“代码隐藏”的代码块中的事件处理程序仅对页面的该迭代有效。处理程序不会在回发过程中保留。因此,当用户选中复选框并且页面自动发布回服务器时,事件处理程序尚未连接。

您需要在页面生命周期<的某个阶段连接事件处理程序< /a> 在回发事件处理阶段之前。您可以在 PageLoad 中执行此操作,也可以在标记中以声明方式执行此操作。

这里类似的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文