問題描述
我正在嘗試在 Visual Studio asp.net 上構(gòu)建一個(gè)程序,但每當(dāng)我嘗試單擊帶有 OnClick 事件的按鈕時(shí),我都會(huì)收到以下錯(cuò)誤:
"CS1061: 'ASP.test_aspx' 不包含 'buttonClick' 的定義,并且找不到接受類型為 'ASP.test_aspx' 的第一個(gè)參數(shù)的擴(kuò)展方法 'buttonClick'(您是否缺少 using 指令或程序集參考?)"
這是我的 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="服務(wù)器"><標(biāo)題></標(biāo)題></頭><身體><form id="form1" runat="server"><asp:Button id="b1" Text="Submit" runat="server" OnClick="buttonClick"/><asp:TextBox id="txt1" runat="server"/></div></表格></身體></html>這是我的代碼:
使用系統(tǒng);使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.UI;使用 System.Web.UI.WebControls;命名空間 MRAApplication{公共部分類測(cè)試:System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}void buttonClick(對(duì)象發(fā)送者,EventArgs e){txt1.Text = "文本";}}}
請(qǐng)保持解釋盡可能簡(jiǎn)單,因?yàn)槲沂蔷幋a新手.感謝您的任何幫助.:)
解決方案 您需要將您的事件處理程序聲明為受保護(hù):
protected void buttonClick(Object sender, EventArgs e){txt1.Text = "文本";}
標(biāo)記本質(zhì)上是一個(gè)繼承自后面代碼的類.為了使成員可以訪問,他們需要受到保護(hù)或公開.
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' 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?)"
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>
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.
這篇關(guān)于Asp.Net 不包含 buttonClick 的定義?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!