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