Asp.Net不包含buttonClick的定义吗? [英] Asp.Net does not contain a definition for buttonClick?

查看:262
本文介绍了Asp.Net不包含buttonClick的定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Visual Studio asp.net上构建程序,但是每当我尝试单击带有OnClick事件的按钮时,都会出现以下错误:

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:

"CS1061:'ASP.test_aspx'不包含'buttonClick'的定义,并且找不到扩展方法'buttonClick'接受类型为'ASP.test_aspx'的第一个参数(您是否缺少using指令或程序集引用?)"

"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?)"

这是我的HTML供参考:

Here is my HTML for reference:

<%@ 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>

这是我后面的代码:

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.

这篇关于Asp.Net不包含buttonClick的定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆