简体   繁体   中英

Link Button in asp.net click event not firing

<asp:LinkButton ID="lbDownloadFile" name = "lbDownloadFile" runat="server" CausesValidation="false" 
                            onclick="lbDownloadFile_Click" />

I have this link button. on click:

protected void lbDownloadFile_Click(object sender, EventArgs e)
{    //here is my debug pointer/breakpoint
    .........................
}

but this event is not firing. my Page_Load() event is firing. How to solve this problem?

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        if (Session[Antrage_AnfrageSessionNames.AgntNr] == null)
        {
            Response.Redirect("../UserSessionError.aspx");
        }

        try
        {
            if (Request.QueryString["Kundennummer"].ToString() != null)
            {
                //If kundennummer exists in QueryString then stores it to further use 
                kundennummer = Request.QueryString["Kundennummer"].ToString();                    
            }
        }
        catch
        {
            kundennummer = string.Empty;
        }
    }
}

EDIT:

I am adding the code, what FireFox firebug shows me respective to the LinkButton. 在此输入图像描述

I think the auto generated href is the main problem here.

@belogix comment is Good

This is part of the normal ASP .NET WebForms Page Lifecycle... Page Load is called EVERY time a postback occurs. After PageLoad your event should then fire... BUT Are you doing anything in Page Load that would stop this from happening?

I think Your Page load method have did anything wrong. May be your link button was reload from page load event.

Sample Error

If you using grid view and also this link button in inside of your grid, You are doing this things

  • Write Grid bind method

  • then you called the grid bind method in page load event

Your code look like now

Page_load()
{
// called here Grid bind method
} 

Now, the grid reload on every post back .

Solution

Now you must need to set !IsPostBack and then call the grid bind method in inside of !IsPostBack

The code look like

Page_load()
{
if(!IsPostBack)
{
// called here Grid bind method

}
} 

This is your problem. and Also it's my guess.

Please tell me if you not use any controls(Gridview,listview,etc)


Edit

Your code is working to me if i don't write any code on page-load event

See

Default.aspx

 <asp:LinkButton ID="lbDownloadFile" Text="he he he" name="lbDownloadFile" runat="server" CausesValidation="false" OnClientClick="lbDownloadFile_Click"
        OnClick="lbDownloadFile_Click" />

and server side code is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      // Empty code 
    }

    protected void lbDownloadFile_Click(object sender, EventArgs e)
    {
    }
}

this is fine for me, So you missed anything in page load event

* OnClientClick and onclick have not any problems. The problems created on pageload event,

Please post your pageload code, otherwise we can't solve it. :)

Edit 2

  • Please check the link button was OUTSIDE of your form elements. This link button should be inside of the form element

  • And your page load event should be

protected void Page_Load(**object sender, EventArgs e**) { //Code }

Not

protected void Page_Load(){}

You have missed

object sender, EventArgs e

I have created demo project and copy same code as you have written.It is working fine

  <asp:LinkButton ID="lbDownloadFile" name = "lbDownloadFile" Text="Click me" runat="server" CausesValidation="false" OnClientClick="lbDownloadFile_Click"
                            onclick="lbDownloadFile_Click" />

in code behind file

  protected void lbDownloadFile_Click(object sender, EventArgs e)
        {    //here is my debug pointer/breakpoint
        }

I have just added text on link button.

只需转到按钮属性并设置即可

UseSubmitBehaviour= False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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