4
<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. enter image description here

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

23
  • are you sure putting same name of event for client and server would work? Commented May 6, 2014 at 11:52
  • no. I didn't put it first. onClientClick was writen there later to try, if it works. Commented May 6, 2014 at 11:53
  • well i suspect it is OnClick not onclick, case sensitive issue might be Commented May 6, 2014 at 11:54
  • 3
    "...instead of this event, my Page_Load() event is firing..." - 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? Commented May 6, 2014 at 11:57
  • 1
    Would you happen to be creating this LinkButton in a GridView, or a similar structure? Event handlers for controls in these structures don't work the way you think they do... Commented May 6, 2014 at 12:06

3 Answers 3

4

@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
Sign up to request clarification or add additional context in comments.

10 Comments

Page_Load event is added. Please check if u can help.
@AbdurRahim Please check the link button was OUTSIDE of my form elements
@AbdurRahim Please now see my Edit 2
i have mistaken to write. they are there. I am looking at your first point. trying to regenerate the aspx.designer.cs file.
yes. It executes fine. to the end brace. then direcly comes to the aspx page, where several data is bound.
|
1

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.

1 Comment

I think the OP have dynamically added the Text . This is not a problem, This problem coming from page-load event. But OP did't put that code. :)
0

Just go to button properties and set

UseSubmitBehaviour= False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.