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.

Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83
  • are you sure putting same name of event for client and server would work? – Ehsan Sajjad May 06 '14 at 11:52
  • no. I didn't put it first. onClientClick was writen there later to try, if it works. – Abdur Rahim May 06 '14 at 11:53
  • well i suspect it is OnClick not onclick, case sensitive issue might be – Ehsan Sajjad May 06 '14 at 11:54
  • `portected` spelling mistake. It should be `protected` – Siva Charan May 06 '14 at 11:54
  • but it would throw syntax error not will even compile – Ehsan Sajjad May 06 '14 at 11:55
  • 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? – Belogix May 06 '14 at 11:57
  • Yes, I understand the page load. Thanks. I am just getting a value from query string in page_load while it's !IsPostBack – Abdur Rahim May 06 '14 at 12:00
  • Can i see your page-load event codes? –  May 06 '14 at 12:01
  • 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... – Steve's a D May 06 '14 at 12:06
  • @Steve `OnClick` event should fire irrespective of place where the control is placed right? – Bharadwaj May 06 '14 at 12:08
  • @Bharadwaj No, when you place these in structures like that you have to use CommandArgument, see the following answer: http://stackoverflow.com/questions/14896125/capture-multiple-values-from-grid-view-to-pass-to-a-stored-procedure/14896507#14896507 – Steve's a D May 06 '14 at 12:10
  • @Steve Its not like "You have to", but like "You can also". `OnClick` will also work for `Buttons` inside `Listing` controls. – Bharadwaj May 06 '14 at 12:11
  • @Steve I am using `OnClick` in all my `GridView`, `DataGridView` and its working :) Besides, `CommandArgument` is just to hold values, `CommandName` will hold what the button is meant for. – Bharadwaj May 06 '14 at 12:13
  • @Bharadwaj MSDN begs to differ...http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx – Steve's a D May 06 '14 at 12:15
  • @Steve that says `Buttons within a GridView control can also invoke some of the built-in functionality of the control.` So its not like "You have to". – Bharadwaj May 06 '14 at 12:18
  • Hey Guy's Please see my answer, It's may be helps to him! :) –  May 06 '14 at 12:19
  • page load event is added. you just ask if u want to know – Abdur Rahim May 06 '14 at 12:23
  • Try removing `OnClientClick="lbDownloadFile_Click"` – Bharadwaj May 06 '14 at 12:23
  • `OnClientClick="lbDownloadFile_Click"` is not the fact. I have tried removing this – Abdur Rahim May 06 '14 at 12:25
  • Then try deleting `Designer.cs` file and regenerate it. – Bharadwaj May 06 '14 at 12:26
  • `Please check the link button was OUTSIDE of my form elements` –  May 06 '14 at 12:27
  • @Bharadwaj After further research it turns out I could be mistaken. Good call. – Steve's a D May 06 '14 at 12:27
  • @AbdurRahim Please see now **Edit 2** in my answer –  May 06 '14 at 12:31

3 Answers3

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
  • Page_Load event is added. Please check if u can help. – Abdur Rahim May 06 '14 at 12:27
  • @AbdurRahim Please check the link button was OUTSIDE of my form elements –  May 06 '14 at 12:27
  • @AbdurRahim Please now see my Edit 2 –  May 06 '14 at 12:29
  • i have mistaken to write. they are there. I am looking at your first point. trying to regenerate the aspx.designer.cs file. – Abdur Rahim May 06 '14 at 12:33
  • yes. It executes fine. to the end brace. then direcly comes to the aspx page, where several data is bound. – Abdur Rahim May 06 '14 at 12:37
  • I am trying to see my designer.cs file. i have found a way `Convert to Web Application`. But will it harm my normal web site? – Abdur Rahim May 06 '14 at 12:38
  • @AbdurRahim did you check `The link button was OUTSIDE of your form elements`? This link button should be inside of the form element –  May 06 '14 at 12:39
  • @AbdurRahim , OOPs I can't get you, What you mean `Convert to Web Application`? Is is not Web Application? –  May 06 '14 at 12:40
  • `Control is inside form element. ` No, Its an web site. – Abdur Rahim May 06 '14 at 12:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52108/discussion-between-user256103-and-abdur-rahim) –  May 06 '14 at 12:45
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.

Deepak Joshi
  • 1,036
  • 7
  • 17
  • 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. :) –  May 06 '14 at 12:21
0

Just go to button properties and set

UseSubmitBehaviour= False
Til
  • 5,150
  • 13
  • 26
  • 34