问题描述
我已经在WebForm1.aspx文件中找到了它:
<asp:checkBox id="ckbxAllGenres" runat="server" Checked="True" OnCheckedChanged="ckbxAllGenres_CheckedChanged" />
<label for="ckbxAllGenres">All</label>
<asp:checkBox id="ckbxAction" runat="server" />
<label for="ckbxAction">Action</label>
<asp:checkBox id="ckbxAdventure" runat="server" />
<label for="ckbxAdventure">Adventure</label>
WebForm1.aspx.cs文件中的事件处理程序为:
protected void ckbxAllGenres_CheckedChanged(object sender,EventArgs e)
{
bool allGenresChecked = ckbxAllGenres.Checked;
ckbxAction.Checked = allGenresChecked;
ckbxAdventure.Checked = allGenresChecked;
. . .
我在第一行有一个断点(对bool的赋值)。
当我检查ckbxAllGenres控件时,未达到断点;因此,当然,代码不会运行,什么也不会发生。
确实,事件处理程序指示“ 0个引用”,但是为什么会这样呢?
WebForm1.aspx的第一行是:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Flix4Fams_WebForms.WebForm1" %>
解决方法
使用Web表单已经有一年了,但是我认为您在复选框上缺少AutoPostBack="true"
:
<asp:checkbox
id="ckbxAllGenres"
runat="server"
Checked="True"
OnCheckedChanged="ckbxAllGenres_CheckedChanged"
AutoPostBack="true" />