Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Web Controls (http://www.velocityreviews.com/forums/f63-asp-net-web-controls.html)
-   -   Button Click event not firing (http://www.velocityreviews.com/forums/t777455-button-click-event-not-firing.html)

carentrica 12-20-2005 12:14 PM

Button Click event not firing
 
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?


Jeff 12-21-2005 03:52 AM

Re: Button Click event not firing
 
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" />


dbottjer 12-21-2005 04:41 AM

RE: Button Click event not firing
 
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 12-21-2005 02:56 PM

RE: Button Click event not firing
 
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?
> >



All times are GMT. The time now is 01:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57