locked
BtnClick event not firing on Page load RRS feed

  • Question

  • User1655374113 posted

    Hello,

    I am having the following code. On the Page load, btnclick method is not firing. What would be the issue ?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler btnClick.Click, AddressOf btnClick_Click
    End Sub
    
    Protected Sub btnClick_Click(sender As Object, e As EventArgs) Handles btnClick.Click
       lblClick.Text = "AddressOf Worked"
    End Sub

    TIA

    Monday, June 3, 2013 10:11 AM

Answers

  • User281315223 posted

    Ah.

    Well typically, I don't believe that you will need to manually create your event handler using AddressOf. In my limited experience with working with Visual Basic, I have always simply used the built-in mapping from OnClick through the markup to the actual event itself.

    'Notice the "Handles btnClick.Click" which is automatically performing your mapping'
    Protected Sub btnClick_Click(sender As Object, e As EventArgs) Handles btnClick.Click
        lblClick.Text = "AddressOf Worked"
    End Sub

    I believe you would only need to use AddressOf if you were creating your button dynamically.

    If you wanted to actually perform your Click event when your page was loaded, I believe you could just call it directly : 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
           'This will call your button click event during the Page_Load event' 
           btnClick.Click(btnClick, New EventArgs())
    End Sub
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 3, 2013 11:10 AM

All replies

  • User163270948 posted

    Is your Page_Load method firing?

    If not, check if you have AutoEventWireup="true" in the Page declaration in your .aspx file.

    Monday, June 3, 2013 10:20 AM
  • User1655374113 posted

    Is your Page_Load method firing?
    Yes, Its firing.

    Monday, June 3, 2013 10:22 AM
  • User1316753262 posted

    Hey,

    Drag a new button and double click or double click on the existing button and check..

    Monday, June 3, 2013 10:24 AM
  • User281315223 posted

    What does the markup for your button look like? 

    The code that you are currently using appears correct (by using the Address to explicitly set the event handler) but it may simply be easier to use the OnClick="BtnClick" property on your button instead.

    <asp:Button ID="btnClick" runat="server" OnClick="btnClick_Click"></asp:Button>

    You could also consider checking out the Properties Window from the Designer View of your form to see if the event is mapped to that function properly.

    Monday, June 3, 2013 10:27 AM
  • User1655374113 posted

    Drag a new button and double click or double click on the existing button and check..

    Tried this one, Not working

    Monday, June 3, 2013 10:36 AM
  • User1655374113 posted

    <asp:Button ID="btnClick" runat="server" OnClick="btnClick_Click"></asp:Button>

    Tried this one too. No luck
    see if the event is mapped to that function properly.

    Can you please explain more on how to check this mapping.

    Monday, June 3, 2013 10:37 AM
  • User281315223 posted

    It looks like that is basically all that you would need. 

    Could you post the rest of the markup that you are using (or whatever you are using currently)?

    Monday, June 3, 2013 10:54 AM
  • User1655374113 posted

    Could you post the rest of the markup that you are using (or whatever you are using currently)?

    Hi, my markup is very simple. I am just trying to understand the use of AddressOf operator at higher level.

    Here is my marup.

     <asp:Button runat="server" Text="Click" ID="btnClick" OnClick="btnClick_Click" />
                            <asp:Label runat="server" Text="" ID="lblClick"></asp:Label>
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       AddHandler btnClick.Click, AddressOf btnClick_Click
      End Sub
    
      Protected Sub btnClick_Click(sender As Object, e As EventArgs) Handles btnClick.Click
        lblClick.Text = "AddressOf Worked"
      End Sub




    Monday, June 3, 2013 11:02 AM
  • User281315223 posted

    Ah.

    Well typically, I don't believe that you will need to manually create your event handler using AddressOf. In my limited experience with working with Visual Basic, I have always simply used the built-in mapping from OnClick through the markup to the actual event itself.

    'Notice the "Handles btnClick.Click" which is automatically performing your mapping'
    Protected Sub btnClick_Click(sender As Object, e As EventArgs) Handles btnClick.Click
        lblClick.Text = "AddressOf Worked"
    End Sub

    I believe you would only need to use AddressOf if you were creating your button dynamically.

    If you wanted to actually perform your Click event when your page was loaded, I believe you could just call it directly : 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
           'This will call your button click event during the Page_Load event' 
           btnClick.Click(btnClick, New EventArgs())
    End Sub
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 3, 2013 11:10 AM
  • User1655374113 posted

    I believe you would only need to use AddressOf if you were creating your button dynamically.

    I am going through this example 

    http://msdn.microsoft.com/en-us/library/y72ewk2b.aspx

    Monday, June 3, 2013 11:22 AM