我正在阅读ASP C#书并完成教程。但是我遇到了一个问题。我有以下代码列出一些事件。
EventTracker.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EventTracker.aspx.cs" Inherits="EventTracker" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Event Tracker</title>
<style type="text/css">
h1
{
font-size: large;
}
</style>
</head>
<body>
<form id="Form1" runat="server">
<div>
<h1>Controls being monitored for change events:</h1>
<asp:TextBox ID="txt" runat="server" AutoPostBack="true"
OnTextChanged="CtrlChanged" />
<br /><br />
<asp:CheckBox ID="chk" runat="server" AutoPostBack="true"
OnCheckedChanged="CtrlChanged"/>
<br /><br />
<asp:RadioButton ID="opt1" runat="server" GroupName="Sample"
AutoPostBack="true" OnCheckedChanged="CtrlChanged"/>
<asp:RadioButton ID="opt2" runat="server" GroupName="Sample"
AutoPostBack="true" OnCheckedChanged="CtrlChanged"/>
<br /><br /><br />
<h1>List of events:</h1>
<asp:ListBox ID="lstEvents" runat="server" Width="355px"
Height="305px" /><br />
</div>
</form>
</body>
</html>
EventTracker.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class EventTracker : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Log("<< Page_Load>>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
//Find the control ID of the sender.
//This requires converting the Object type into a control class.
string ctrlName = ((Control)sender).ID;
Log(ctrlName + " Changed");
}
protected void Log(string entry)
{
lstEvents.Items.Add(entry);
//Select the last item to scroll the list so the most recent are visible
lstEvents.SelectedIndex = lstEvents.Items.Count - 1;
}
}
我收到以下错误:
错误1'ASP.eventtracker_aspx'不包含的定义 'CtrlChanged'并且没有扩展方法'CtrlChanged'接受第一个 可以找到“ASP.eventtracker_aspx”类型的参数(是吗? 缺少using指令或程序集引用?)
我是ASP的新手,想了解为什么会发生这样的错误,所以在将来的参考资料中,我知道是什么导致了它。
答案 0 :(得分:3)
在你的标记中你有
OnCheckedChanged="CtrlChanged"
但是在代码隐藏中没有具有该名称/匹配签名的事件处理程序......
每次CheckBox更改时都会触发此事件,但代码中没有任何内容会响应。
你需要像
这样的东西protected void CtrlChanged(object sender, EventArgs e)
{ // do something }
答案 1 :(得分:1)
您缺少CtrlChanged()
的事件处理程序。
protected void CtrlChanged(object sender, EventArgs e)
{
//....handle event
}