Link Button in asp.net click event not firing

2019-06-25 21:32发布

问题:

<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.

回答1:

@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


回答2:

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.



回答3:

Just go to button properties and set

UseSubmitBehaviour= False