Button Click event not firing

Discussion in 'ASP .Net Web Controls' started by carentrica, Dec 20, 2005.

  1. carentrica

    carentrica Guest

    For some reason, my "testButton_Click()" event handler is not being called
    when testButton is clicked.
    This Button and a Label are the only controls on a Web UserControl which is
    loaded and displayed in the main document. When the button is clicked, a
    postback occurs and the Page events get called, but not the Button Click
    event.

    public class LoginControl : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Button testButton;
    private bool loggedIn = false;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    if (Session["LoggedIn"] != null) {
    loggedIn = (bool)Session["LoggedIn"];
    }
    }

    private void testButton_Click(object sender, System.EventArgs e) {
    // this event handler is never called!
    loggedIn = true;
    }

    private void Page_PreRender(object sender, System.EventArgs e) {
    defaultWebForm defaultPage = (defaultWebForm)Context.Handler;
    // Method to switch the menu between "Log In" and "Log Out".
    defaultPage.ShowLoggedIn(loggedIn);
    }

    private void Page_Unload(object sender, System.EventArgs e) {
    Session["LoggedIn"] = loggedIn;
    }
    }

    In the InitializeComponent method, the event is wired...

    this.testButton.Click += new
    System.EventHandler(this.testButton_Click);

    Can anyone please tell me why?
     
    carentrica, Dec 20, 2005
    #1
    1. Advertising

  2. carentrica

    Jeff Guest

    Im not too familiar with C# or its dynamic allocation of handlers.
    However. if you dont need to set the handlers dynamically, you might
    want to just try setting the handler via buttons 'onClick' property

    <asp:Button id="testButton" Text="Submit" onclick="testButton_Click"
    runat="server" />
     
    Jeff, Dec 21, 2005
    #2
    1. Advertising

  3. carentrica

    dbottjer Guest

    I know you said the event is wired in the InitializeComponent but you might
    want to double check and make sure you haven't lost the binding.

    "carentrica" wrote:

    > For some reason, my "testButton_Click()" event handler is not being called
    > when testButton is clicked.
    > This Button and a Label are the only controls on a Web UserControl which is
    > loaded and displayed in the main document. When the button is clicked, a
    > postback occurs and the Page events get called, but not the Button Click
    > event.
    >
    > public class LoginControl : System.Web.UI.UserControl
    > {
    > protected System.Web.UI.WebControls.Label Label1;
    > protected System.Web.UI.WebControls.Button testButton;
    > private bool loggedIn = false;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > // Put user code to initialize the page here
    > if (Session["LoggedIn"] != null) {
    > loggedIn = (bool)Session["LoggedIn"];
    > }
    > }
    >
    > private void testButton_Click(object sender, System.EventArgs e) {
    > // this event handler is never called!
    > loggedIn = true;
    > }
    >
    > private void Page_PreRender(object sender, System.EventArgs e) {
    > defaultWebForm defaultPage = (defaultWebForm)Context.Handler;
    > // Method to switch the menu between "Log In" and "Log Out".
    > defaultPage.ShowLoggedIn(loggedIn);
    > }
    >
    > private void Page_Unload(object sender, System.EventArgs e) {
    > Session["LoggedIn"] = loggedIn;
    > }
    > }
    >
    > In the InitializeComponent method, the event is wired...
    >
    > this.testButton.Click += new
    > System.EventHandler(this.testButton_Click);
    >
    > Can anyone please tell me why?
    >
     
    dbottjer, Dec 21, 2005
    #3
  4. carentrica

    carentrica Guest

    I think I've found the problem, guys. It seems to be something to do with the
    order in which events fire. The Page Load event fires before the Control
    events and my logic was reloading the Control each time instead of testing
    that the QueryString didn't contain the value that would load the control.
    I've just re-written all the relevant logic and things appear to working as
    expected.

    Many thanks for all your replies.

    "dbottjer" wrote:

    > I know you said the event is wired in the InitializeComponent but you might
    > want to double check and make sure you haven't lost the binding.
    >
    > "carentrica" wrote:
    >
    > > For some reason, my "testButton_Click()" event handler is not being called
    > > when testButton is clicked.
    > > This Button and a Label are the only controls on a Web UserControl which is
    > > loaded and displayed in the main document. When the button is clicked, a
    > > postback occurs and the Page events get called, but not the Button Click
    > > event.
    > >
    > > public class LoginControl : System.Web.UI.UserControl
    > > {
    > > protected System.Web.UI.WebControls.Label Label1;
    > > protected System.Web.UI.WebControls.Button testButton;
    > > private bool loggedIn = false;
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > > // Put user code to initialize the page here
    > > if (Session["LoggedIn"] != null) {
    > > loggedIn = (bool)Session["LoggedIn"];
    > > }
    > > }
    > >
    > > private void testButton_Click(object sender, System.EventArgs e) {
    > > // this event handler is never called!
    > > loggedIn = true;
    > > }
    > >
    > > private void Page_PreRender(object sender, System.EventArgs e) {
    > > defaultWebForm defaultPage = (defaultWebForm)Context.Handler;
    > > // Method to switch the menu between "Log In" and "Log Out".
    > > defaultPage.ShowLoggedIn(loggedIn);
    > > }
    > >
    > > private void Page_Unload(object sender, System.EventArgs e) {
    > > Session["LoggedIn"] = loggedIn;
    > > }
    > > }
    > >
    > > In the InitializeComponent method, the event is wired...
    > >
    > > this.testButton.Click += new
    > > System.EventHandler(this.testButton_Click);
    > >
    > > Can anyone please tell me why?
    > >
     
    carentrica, Dec 21, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    936
    Purvi T
    Oct 19, 2004
  2. Ken Cox [Microsoft MVP]

    Button Click event not firing

    Ken Cox [Microsoft MVP], Apr 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,576
    Andrea Williams
    Apr 28, 2004
  3. Amy
    Replies:
    0
    Views:
    1,042
  4. GauravGupta
    Replies:
    4
    Views:
    2,157
    Shilpa1234
    Jan 19, 2012
  5. Max2006
    Replies:
    2
    Views:
    1,640
    Walter Wang [MSFT]
    Sep 19, 2007
Loading...

Share This Page