简体   繁体   English

Asp.Net不包含buttonClick的定义吗?

[英]Asp.Net does not contain a definition for buttonClick?

I am attempting to build a program on visual studio asp.net but whenever I try to click a button with an OnClick event I get the following error: 我试图在Visual Studio asp.net上构建程序,但是每当尝试单击带有OnClick事件的按钮时,都会出现以下错误:

"CS1061: 'ASP.test_aspx' does not contain a definition for 'buttonClick' and no extension method 'buttonClick' accepting a first argument of type 'ASP.test_aspx' could be found (are you missing a using directive or an assembly reference?)" “ CS1061:'ASP.test_aspx'不包含'buttonClick'的定义,并且找不到扩展方法'buttonClick'接受类型为'ASP.test_aspx'的第一个参数(您是否缺少using指令或程序集引用? )”

Here is my HTML for reference: 这是我的HTML供参考:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="MRAApplication.test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button id="b1" Text="Submit" runat="server" OnClick="buttonClick" />
    <asp:TextBox id="txt1" runat="server" />
</div>
</form>
</body>
</html>

and here is my code behind: 这是我的代码背后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MRAApplication
{
public partial class test : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    void buttonClick(Object sender, EventArgs e)
    {
        txt1.Text = "Text";
    }
}
}

Please keep explanations as simple as possible as I am new to coding. 请保持解释尽可能简单,因为我是编码新手。 Thank you for any and all help. 感谢您提供的所有帮助。 :) :)

You need to declare your event handler as protected: 您需要将事件处理程序声明为受保护的:

protected void buttonClick(Object sender, EventArgs e)
{
    txt1.Text = "Text";
}

The markup is essentially a class that inherits from the code behind. 标记本质上是一个从后面的代码继承的类。 In order for members to be accessible, they need to be protected or public. 为了使成员可以访问,需要对其进行保护或公开。

You have to make it at least protected: 您必须使其至少受到保护:

protected void buttonClick(Object sender, EventArgs e)
{
    txt1.Text = "Text";
}

The default access for everything in C# is "the most restricted access you could declare for that member", so private for a method. C#中所有内容的默认访问权限是“您可以为该成员声明的最严格的访问权限”,因此对于方法来说是private的。

Since the aspx is a child class of your codebehind class( Inherits ) any method that you want to access from the aspx must be declared as protected or public (at least in C#, VB.NET has Handles ). 由于aspx是您的codebehind类( Inherits )的子类,因此您要从aspx访问的任何方法都必须声明为protectedpublic (至少在C#中,VB.NET具有Handles )。

Read: 读:

You need to declare your event handler, you can do that a couple ways: 您需要声明您的事件处理程序,可以通过以下几种方法进行操作:

protected void Page_Load(object sender, EventArgs e)
{
    btnNote.Click += new EventHandler(btnNote_Click);
}

void btnAddNote_Click(object sender, EventArgs e)
{
     // Do Stuff.
}

So as you can see by declaring the event at Page Load , you can use the raw void like you have above. 因此,通过在Page Load中声明事件可以看到,可以像上面一样使用原始void Otherwise you'll need declare it in a protected . 否则,您需要在protected声明它。

protected void btnAddNote_Click(object sender, EventArgs e)
{
    // Do Stuff.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM