简体   繁体   English

ASP.net LinkBut​​ton与代码中的onclick事件不起作用

[英]ASP.net Linkbutton with onclick event in codebehind not working

I have an asp linkbutton in a navigation drop down list. 我在导航下拉列表中有一个asp linkbutton。 It will be used for logging out and so I have an event linked to the code behind, where I shall close and empty the session. 它将用于注销,因此我有一个事件链接到后面的代码,在该事件中我将关闭并清空会话。 But when I startup my project it gives this error: 但是,当我启动我的项目时,它会出现以下错误:

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

this is my aspx code 这是我的aspx代码

<li><asp:LinkButton id="myLink" Text="Logout" OnClick="LinkButton_Click" runat="server"/></li>

and this is the code behind code 这是代码背后的代码

void LinkButton_Click(Object sender, EventArgs e) 
    {
        Session.Clear();
        Session.RemoveAll();
        Session.Abandon();
        Response.Redirect("Login.aspx"); 
    }

Problem : You should specify protected access modifier for LinkButton Click Event otherwise by default access modifier private will be applied and LinkButton Click event function can not be accessed. 问题:您应为LinkButton Click事件指定protected访问修饰符,否则默认情况下将应用private访问修饰符,并且无法访问LinkButton Click事件功能。 that is why t=you are getting the following Exception 这就是为什么t =您得到以下异常

'ASP.main_aspx' does not contain a definition for 'LinkButton_Click'...... “ ASP.main_aspx”不包含“ LinkBut​​ton_Click”的定义……

Solution : add protected as access-modifier for your LinkButton Click event in the code behind file 解决方案:在文件后面的代码中为LinkButton Click事件添加protected作为access-modifier

Try This : 尝试这个 :

protected void LinkButton_Click(Object sender, EventArgs e) 
{
   Session.Clear();
   Session.RemoveAll();
   Session.Abandon();
   Response.Redirect("Login.aspx"); 
 }

I have found it, I made a silly mistake. 我找到了,我犯了一个愚蠢的错误。 I made this OUTSIDE of my form elements with the runatserver in it. 我将其中的runatserver放在表单元素的外面。

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

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