Why does a button's click event seem to fire AFTER Page_Load?

Once again - confused. :)  I must be doing something wrong, because I simply can't understand why a page posts before a button could do anything.

 I'm trying to do the following concept:

I have a gridview, filled with data, and there is one dataset with 2 tables saved in a session variable.  There are 2 link buttons that will change the grid's data source to one of the datatables. Ex. You click one button, the grid fills with datatable #1. You click the other button, the grid fills with datatable #2.

I put breakpoints on the Page_Load, and each button's click event. After the button is clicked, I thought it would go to that event - where I can change a session variable that the page_load could then use to fill the grid correctly. But instead, the page loads, THEN the click event fires - changing my variable. Then clicking it again, the grid loads correctly, but now the session variable is changed. So it takes 3 clicks to see the page correctly.

I come from VB.NET Windows programming - and not that great with ASP.NET. But, isn't a button's click event supposed to fire when it's clicked?

 

Thanks for the help,
Denvas

0
Denvas
12/4/2006 8:56:44 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

5 Replies
900 Views

Similar Articles

[PageSpeed] 7
Get it on Google Play
Get it on Apple App Store

can you show us two things:

1.  The html code for the button in question

2.  The code you have in your .vb file (or .cs file if your using C#) that is for that button.

is the button a <asp:Button> or a regular html button? does it have the runat="server" property in its tag if its server button?

thanks,
mcm

0
mcmcomasp
12/4/2006 9:07:16 PM

Link buttons: 

<asp:LinkButton ID="lnkReferrals" runat="server">0</asp:LinkButton>

<

asp:LinkButton ID="lnkPaid" runat="server">$ 0.00</asp:LinkButton>

Regular button:

<

asp:Button ID="btnFilter" runat="server" Text="Filter" Width="81px" />

Code for each:

Protected Sub lnkReferrals_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkReferrals.Click
        Try
            Session.Item("CurGridView") = 1
            Session.Item("SetCalendarBounds") = True
        Catch ex As Exception
            With lblMessage
                .Text = ex.Message
                .Visible = True
            End With
        End Try
    End Sub

    Protected Sub lnkPaid_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPaid.Click
        Try
            Session.Item("CurGridView") = 0
            Session.Item("SetCalendarBounds") = True
        Catch ex As Exception
            With lblMessage
                .Text = ex.Message
                .Visible = True
            End With
        End Try
    End Sub

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        Try
            Session.Item("SetCalendarBounds") = False
        Catch ex As Exception
            With lblMessage
                .Text = ex.Message
                .Visible = True
            End With
        End Try
    End Sub

 

None of these events fire before leaving the page. Any ideas?

I appreciate the help.

0
Denvas
12/4/2006 9:25:46 PM

Link buttons: 

<asp:LinkButton ID="lnkReferrals" runat="server">0</asp:LinkButton>

<

asp:LinkButton ID="lnkPaid" runat="server">$ 0.00</asp:LinkButton>

Regular button:

<

asp:Button ID="btnFilter" runat="server" Text="Filter" Width="81px" />

Code for each:

Protected Sub lnkReferrals_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkReferrals.Click
        Try
            Session.Item("CurGridView") = 1
            Session.Item("SetCalendarBounds") = True
        Catch ex As Exception
            With lblMessage
                .Text = ex.Message
                .Visible = True
            End With
        End Try
    End Sub

    Protected Sub lnkPaid_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPaid.Click
        Try
            Session.Item("CurGridView") = 0
            Session.Item("SetCalendarBounds") = True
        Catch ex As Exception
            With lblMessage
                .Text = ex.Message
                .Visible = True
            End With
        End Try
    End Sub

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        Try
            Session.Item("SetCalendarBounds") = False
        Catch ex As Exception
            With lblMessage
                .Text = ex.Message
                .Visible = True
            End With
        End Try
    End Sub

 

None of these events fire before leaving the page. Any ideas?

I appreciate the help.

0
Denvas
12/4/2006 9:26:24 PM

That is actually the design of it.

Page_Load is executed on every request, including first-request and postbacks, and then postback event handlers are executed.

For precise event execution orders, refer to this http://msdn2.microsoft.com/en-us/library/ms178472.aspx


Debugger is my best friend. (http://haoest.info)
0
haoest
12/4/2006 9:28:15 PM

Sorry all about the double posting of my code sample.  My browser looked like it crashed, and I hit the post button again.

Thanks for the link haoest!!!!  I put my code in the Page_PreRender event - and it works EXACTLY the way I want now. 

Maybe it's just me, but wouldn't it work better (and less confusing) if the event was treated like a submit, and sent to the server, where it can process for the next page load?  For my example - the event sets a session variable, that the next page load can see. Seems just a bit backwards to me. But I come from Windows Apps, where everything is a bit linear in nature. So I just need to train myself to the abstract world of ASP. LOL

To Everyone: If it wasn't for this forum, and all you great and patient fellow programmers - I'd be lost amongst a sea of Google searches with Viruses lurking around every corner.

So, for real - thanks all, and especially www.asp.net

Denvas

0
Denvas
12/4/2006 11:05:06 PM
Reply:

Similar Artilces:

__doPostBack('btn', 'Click'); not firing button click event
Hi, I have a javascript function function refreshGrid(){ __doPostBack('btn', 'Click'); }  I was under the impression that calling this would cause the button click handler to be fired.   client side: <asp:Button ID="btn" runat="server" /> Server side: Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click Response.Write("hello world") End Sub What am I doing wrong for this not to be fired? Hi, I think u need to attach the OnClick ev...

An OnClick event doesn't fire up when a button is clicked in a Tab Panel in a Tab Container in an AJAX Web Form
Hello Post: I would like to enclose the following code so you can analize what is going on: "> " style="vertical-align:top;"> "> "> ...

Problem with clicking enter from a texbox firing default button's click event
So I've been really playing around a lot with ASP.Net 2.0 and ran into an odd problem.I have a GridView displaying a list of customers from Northwind db, and I have the first column of the grid displaying a "select" button.  When this button is clicked, the FormView control on the page switches from Insert mode to Edit mode and displays the selected Customer's information to edit.  All that works fine.But then I created a custom pager User Control that among other things has a small textbox allowing the user to type-in the page number and press enter to display th...

need help, when clicking on button1, it fires the another button's click event?
Hi Guys,I am using asp.net 2 and vs.net 2005.I have a simple page with datagrid and there button -- "Show" "Hide" "Refresh"Click on "Show" to display the datagridClick on "Hide" to hide the datagridClick on "Refresh" to refresh the datagrid data"Show" button and "Hide" button are woking fine.The problem is "Refresh" button and when I click on "Refresh" button, the data refresh is ok. However, the datagrid is dispear. It seems like when clicking on button "Refresh", it fire the "Hide" button's click eventl. Does anyone have this problems before?Thsnks in advance...Justin If the datagrid ref...

Gridview Event : image button's row command fire second time when i do refresh or postback after clicking on imgdelete button immidiatly
 i am getting  imgDelete (image button) row command fire second time when i do refresh or postback after clicking on imgdelete button immidiatly, so how can i prevent(handle) it from firing second time.it is a user control   fun.ascx.vb Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load                       If IsPostBack = False Then            FillGridView()   &nbs...

ISSUE WITH Control's behaviour inisde UPDATE PANEL
 hii evry1..,             i have an strange issue...here..plzz chk it out..                      aspx page:            < asp:UpdatePanel ID="OUpdatePanel" runat="server" >                <ContentTemplate>             &nbs...

want to fire other button's click event
I am developing asp.net web applications and I have several button(btn1, btn2, btn3, btn4, btn5) on my webform which performs various tasks Now I want create temparary button named "TemporaryButton" and what I would like to do is when someone clicks on this button then I want to fire btn1's click event tell me how do I do that urgently required thank u maxmax create another method... have both buttons call this method. Otherwise, just go to the <asp:button...> and add an OnClick="otherbutton_click" and it should fire the other event. you mean just...

Programmatically fire a button's click event
I need to fire a button click event during some processing server-side. I do not need to wire up an extra event handler, cannot use JavaScript for this problem, but need to actually fire the click event itself. Is there a way to do this? It's not how hard you push in life, but who you push, that makes the difference between success and running for your life. You could just call the event handler directly rather than trying to raise the event, but if you do want to raise the buttons server side click event, there a solution in this post: http://forums.asp.net/p/1046550/26...

How to trigger the button's Click event only when user really click it. (Escape the return button)
Hi, I want the user really click the button, then exec the code in btnSubmit_Click event . Not by pushing the <return> button. Does anybody have any idea about this? Thanks Create a separate Subroutine that does all the stuff you need the click event to do (separate from the Button's click event)Then, in the button's click event - call that event. At this point, you can also call it from anywhere else in your application that you'd like.David WierMCP/ASPInsiderASPNet101.com - where to look first!Please Vote for ASPNet101 - 'Best Community Resource'!Control Grouper ...

(web control button) click event doesn't fire
Hi, I've problem with simple case ... On my web page I have 2 user controls (one is logging control which has button and its onClick event works fine ... next control has also button and its onClick event doesn't fire) all handler are initialize correct and I've no idea why the next buttons (it happens to every next button that I try to insert on web page) bevave like don't have assemblied handlers :/ Anyone had problem like that ? Lukas         paste the code where the handler is getting hooked in the code behind file. It is in the section named Visual Studio gen...

How To Cause a Button's Click Event to Fire from Server Side
I need to cause a Buttons Event to Fire should a criteria Not be met, How can I do this in Server code? Thanks in Advance.Net Web/Software Engineer You can't actually cause the event to fire, but you can certainly call the handler just as you would any other method.Thanks, EdMicrosoft MVP - ASP/ASP.NET   I am a newbie when it comes to Events and Delegates. What I want to do with this cause this event to fire whenever a certain a criteria is met. How do I go about doing this ??? thanks.Net Web/Software Engineer Like I said, you can simply call the event handler like this: ...

Button's click event handler fires every other time
I have a button in a user control that is contained in a page.  The first time the user control loads, and I fill out my form and click my button, it fires normally.  So I change my input data, and click the button again, and... nothing happens.  The event handler never fires.  The data that I put in the form does disappear though.  So I put data back in the form and click the button again, and it works.  It works every other time I click it.  What is going on?! Here's some code snippets: From my .aspx page (snipped to show just the inclusion of ...

Table Cell click event
Hi, I used to have a custom server control inhirted from TableCell control to implement click event in VS.NET 2003.  When I use the custom control in VS.NET 2003 Web application (dynamically adding to table object), everything works fine.  Below is the code from 2003 project, and everything works good in this way.                 Dim c As New MyCustomTableCell                  c.Width = 200     &nb...

I've added button withevent into row of gridview but when i click it , click event didn't fire.
I've add button withevent in empty row of gridview but when i click it , click event didn't fire. what's worng is it? My code is: Partial Class dc_so Inherits System.Web.UI.Page Dim WithEvents btnAdd As New Button Dim txbSoId As New TextBox Dim txbCartonName As New TextBox Protected Sub gvSo_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSo.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then ' check for empty data If Convert.ToString(DataBinder.Eval(e.Row.DataItem, "so_id")).ToString.Trim = "" Then btnAdd.Text...

Web resources about - Why does a button's click event seem to fire AFTER Page_Load? - asp.net.web-forms

Resources last updated: 12/24/2015 10:30:05 AM