简单问题-受保护级别限制(C#)

zsww 2003-09-30 11:37:35
编译出现以下错误:

不可访问“WebApplication3.WebForm1.Button1_Click(object, System.EventArgs)”,因为它受保护级别限制

<asp:Button id="Button1" runat="server" Text="提交" onClick="Button1_Click"></asp:Button>

private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text="您选择了" + TextBox1.Text + DropDownList1.SelectedItem;
}


什么原因?请帮忙解决!
...全文
288 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zsww 2003-10-08
  • 打赏
  • 举报
回复
难道没有人解释这个问题吗???
zsww 2003-10-06
  • 打赏
  • 举报
回复
Meyer() 理解错了,我是为了方便所以没有把.aspx文件和.aspx.cs文件的代码分开!详细代码在下面:

--------------webform1.aspx-----------------------------
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P align="center">
<FONT face="宋体">品牌:
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox> 
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="请输入品牌" ControlToValidate="TextBox1"></asp:RequiredFieldValidator></FONT></P>
<P align="center"><FONT face="宋体"> 产品类型:
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="请选择服务器">请选择服务器</asp:ListItem>
<asp:ListItem Value="服务器">服务器</asp:ListItem>
<asp:ListItem Value="笔记本">笔记本</asp:ListItem>
<asp:ListItem Value="台式机">台式机</asp:ListItem>
</asp:DropDownList>  
<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="请选择服务器" ControlToValidate="DropDownList1"
InitialValue="请选择服务器"></asp:RequiredFieldValidator></FONT></P>
<P align="center"><FONT face="宋体">
<asp:Button id="Button1" runat="server" Text="提交" OnClick="Button1_Click"></asp:Button></P>
</FONT>
<P align="center"><FONT face="宋体">
<asp:Label id="Label1" runat="server"></asp:Label></P>
</FONT>
</form>
</body>
</HTML>

------------------------------webform1.aspx.cs-----------------------
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;

namespace WebApplication3
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面

}

#region Web 窗体设计器生成的代码

private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text="您选择了" + TextBox1.Text + DropDownList1.SelectedItem;
}
}
}

编译出现以下错误:

不可访问“WebApplication3.WebForm1.Button1_Click(object, System.EventArgs)”,因为它受保护级别限制

小弟刚学C# 望高手指点!

libjprayerlibj 2003-10-06
  • 打赏
  • 举报
回复
protected 或者 public 都可以啊
Meyer 2003-10-06
  • 打赏
  • 举报
回复
不知何谓完美答案
只觉得你该看看语法方面的东东

<asp:Button id="Button1" runat="server" Text="提交" onClick="Button1_Click"></asp:Button>

————————aspx----------------------------

private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text="您选择了" + TextBox1.Text + DropDownList1.SelectedItem;
}

--------------------- .aspx.cs------------------------

如果这样布局,是肯定不能你那样用的

但是你如果在 .aspx.cs文件中使用event那样是没有问题的
zsww 2003-10-06
  • 打赏
  • 举报
回复
没人回答嘛???

我再加10分!
xhan2000 2003-09-30
  • 打赏
  • 举报
回复
只有protected或public可以被继承类访问到
cyp503 2003-09-30
  • 打赏
  • 举报
回复
private

==〉

protected

or

public
zsww 2003-09-30
  • 打赏
  • 举报
回复
我试了 编译以后是可以的!

但是好像用private也是可以得啊 :((

望高手给一个完美的答案!

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧