ASP.NET按钮单击事件错误

时间:2013-05-20 12:13:59

标签: c# asp.net events event-handling

我有一个带有按钮的Main.Master页面 在“设计”视图中,我双击该按钮,在Main.Master页面中创建一个点击事件,如下所示;

<script runat="server">

    protected void btnKayit_Click(object sender, EventArgs e)
    {

    }
</script>

我希望它在Main.Master.cs页面创建,当我这样做时,它给了我这个错误;

CS1061: 'ASP.main_master' does not contain a definition for 'btnKayit_Click' and no extension method 'btnKayit_Click' accepting a first argument of type 'ASP.main_master' could be found (are you missing a using directive or an assembly reference?)

我该如何解决这个问题?

编辑:aspx代码;

<asp:Button CssClass="arama" ID="btnKayit" runat="server" Text="Kayıt Ol" Height="30px"
                                    Width="75px" Style="margin: 0px; float: left; width: 75px; padding: 0px; margin-bottom: 0px;
                                    color: #d55355; font-size: 12px" onclick="btnKayit_Click" />

2 个答案:

答案 0 :(得分:0)

验证.master和.master.cs文件中的命名空间。

检查主文件的指令......

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Main.Master.css"
Inherits="Main" EnableTheming="true" %>

这里,检查CodeFile属性的名称,它应该与Master.cs文件的类名相同

public partial class Main : System.Web.UI.MasterPage

答案 1 :(得分:0)

根据您的错误消息,我认为您的母版页继承了ASP.main_master类。您需要在btnKayit_Click()课程中添加ASP.main_master。基本上,您的代码应如下所示。

HTML:

//Please note the inherits attribute
<%@ Master Language="C#" Inherits="main_master" ... %>


<asp:Button CssClass="arama" ID="btnKayit" runat="server" ... 
            onclick="btnKayit_Click" />

代码背后:

//Please note the class name here it should be same as inheriting class name
public partial class main_master : System.Web.UI.MasterPage
{
    //....

    protected void btnKayit_Click(Object sender, System.EventArgs e)
    {
    }
}