事件处理程序不使用AddHandler进行触发 [英] Event handler not firing using AddHandler

查看:253
本文介绍了事件处理程序不使用AddHandler进行触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个约40个复选框的表单。选中一个复选框后,div控件的属性应该从none更改为block,反之亦然。我没有收到错误,但是checkedchanged事件没有被处理。以下是标记:

 < tr> 
< td class =sectionSubHeader lightgrey>
< asp:CheckBox ID =chkbxC​​OMAEFundAutoPostBack =truerunat =server/>
COM学术卓越基金 - 已授予
< / td>
< / tr>
< tr>
< td>
< ul class =boldDetail>
< li>财务需求< / li>
< / ul>
< / td>
< / tr>
< tr>
< td colspan =2class =subSectionPad>说明..< / td>
< / tr>
< tr>
< td colspan =2class =subSectionPad>
< asp:Label ID =lblCOMAEFundrunat =server>< / asp:Label>< br />
< div id =divCOMAEFundrunat =server>
< asp:TextBox ID =txtCOMAEFundrunat =serverTextMode =MultiLineColumns =95Rows =4>< / asp:TextBox>
< / div>
< / td>
< / tr>

这是codebehind:

  Dim temp As String 
Dim div As HtmlControl

对于每个ctrl作为控件在wizard.WizardSteps
对于每个子控件作为控件在ctrl.Controls
如果TypeOf(subCtrl)是CheckBox然后
temp = subCtrl.ID.Replace(chkbx,div)
div = wizard.FindControl(temp)
div。 Style(display)=none
AddHandler CType(subCtrl,CheckBox).CheckedChanged,AddressOf Chkbx_CheckChanged
End If
下一个
下一个

这是子

 私人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)

如果sender.Checked = True那么divCtrl.Style(di splay)=blockElse divCtrl.Style(display)=none

End Sub


解决方案

您附加在代码块中的事件处理程序只会对该页面的迭代生效。处理程序不会在回发后持久化。所以当用户检查一个复选框,页面被自动发回服务器时,事件处理程序没有被接线。



你需要有事件处理程序在回发事件之前的页面生命周期中的某个阶段连线处理阶段您可以在PageLoad或声明性地在您的标记中执行此操作。



这里有一个类似的问题。


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

解决方案

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.

这篇关于事件处理程序不使用AddHandler进行触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆