我有一个包含大约40个复选框的表单。选中复选框后,div控件的属性应从“none”更改为“block”,反之亦然。我没有收到错误,但未处理checkedchanged事件。这是标记:
<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
答案 0 :(得分:7)
您在标记为“代码隐藏”的代码块中附加的事件处理程序仅对该页面的迭代有效。处理程序不会在回发中持久存在。因此,当用户选中一个复选框并且页面自动发回服务器时,事件处理程序尚未连接。
在回发事件处理阶段之前,您需要在page life cycle的某个阶段连接事件处理程序。您可以在PageLoad中或在标记中以声明方式执行此操作。
Here's一个类似的问题。