Button OnClick event not firing

  • Thread starter Thread starter Daisy
  • Start date Start date
D

Daisy

In my user control:

<asp:Button id="btnLogin" runat="server" Text="Login"></asp:Button>

And the code-behind:

protected override void OnInit(EventArgs e)
{
btnLogin.EnableViewState = true;
btnLogin.Click += new EventHandler(btnLogin_Click);
base.OnInit(e);
}

public void btnLogin_Click(object sender, EventArgs e)
{
throw new Exception("Worked!");
}


I tried enabling viewstate (I was told events don't work without it),
because I've set it to default to false in web.config, but still, it never
fires. How do I debug something like this? :-\

It's posting back fine, just never hitting the code in the Click method
 
I honestly have to admit being a bit of an amateur with ASP.NET, but my
undertstading is that ViewState has nothing to do with events. ViewState
simply records previous states of controls so that during postback the
controls will maintain their state (for example, the contents of a listbox
does not have to be repopulated at postback if ViewState is enabled).

That said, I'm not sure what could be causing your button not to work.

Pete
 
Daisy said:
In my user control:

<asp:Button id="btnLogin" runat="server" Text="Login"></asp:Button>

And the code-behind:

protected override void OnInit(EventArgs e)
{
btnLogin.EnableViewState = true;
btnLogin.Click += new EventHandler(btnLogin_Click);
base.OnInit(e);
}

public void btnLogin_Click(object sender, EventArgs e)
{
throw new Exception("Worked!");
}


I tried enabling viewstate (I was told events don't work without it),
because I've set it to default to false in web.config, but still, it never
fires. How do I debug something like this? :-\

It's posting back fine, just never hitting the code in the Click method

Oh, and the asp:TextBox's that are next to it aren't holding their values
across postbacks, so whatever is causing that, is probably the same as
what's causing my events not to fire? :-\
 
Pete Davis said:
I honestly have to admit being a bit of an amateur with ASP.NET, but my
undertstading is that ViewState has nothing to do with events. ViewState
simply records previous states of controls so that during postback the
controls will maintain their state (for example, the contents of a listbox
does not have to be repopulated at postback if ViewState is enabled).

I didn't think so too, I'm just clutching at straws :O(
 
<snip>

Solved it. The control containing the button was inside Head.ascx, and I
forgot that that one's loaded with LoadControl by the derived page class,
and was done in PreRender. Moved it to OnInit and all works fine :)
 
Back
Top