7

I have an ASP.NET (C#) page with some 3rd party controls, some ajaxy stuff and some normal ASP.NET Button controls.

The Button click events do not fire when clicked.

Double-clicking the button in design mode in VS 2008 switches to the code-behind but doesn't create the event handler.

Creating the event handler manually doesn't help.

The whole page is too big to include here, but this is the top bit:

<%@ Page Language="C#" MasterPageFile="~/basewidepage2.master" AutoEventWireup="true" EnableEventValidation="false" CodeFile="CompanyCompliance.aspx.cs" Inherits="CompanyCompliancePage" Title="3DSS" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%@ Register Assembly="obout_Grid_NET" Namespace="Obout.Grid" TagPrefix="cc2" %>
<%@ Register Src="usercontrols/CalendarEx.ascx" TagName="CalendarEx" TagPrefix="uc2" %>
<%@ MasterType VirtualPath="~/basewidepage2.master" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceholder1" runat="Server">

    <script type="text/javascript">
         // a bunch of function declarations
    </script>

...and my button declaration on the page:

<asp:Button ID="LicenseCsvButton" runat="server" Text="Export to CSV" OnClick="LicenseCsvButton_Click" />

...and the code-behind:

protected void LicenseCsvButton_Click(object sender, EventArgs e)
{
    // get data
    CompanyCompliance cc = new CompanyCompliance(Master.theCompany.ID);
    DataTable dt = cc.BusinessLicenses.Tables[0];

    // send to browser as download
    Tools.SendTableAsCsvToBrowser(Response, dt, "LicenseData");
}

Any ideas? could it be the ajax or something? Maybe the 3rd party "obout" grid control?

Update:
I did fix this a month or two ago, so I came back to this question to answer it for posterity but couldn't remember exactly how I fixed it! (Curse you, old age!) I had some limited success by replacing some of the ASP.NET AJAX controls with jQuery UI ones but I think the real solution was that one of the properties in the one of the tags was pointing to a control that no longer existed.

If you're in this same situation, try that and let me know what works in the comments and I'll post the correct answer.

MGOwen
  • 6,562
  • 13
  • 58
  • 67
  • 1
    Is there any JS which could be intercepting and canceling the form submission? – Rex M Sep 16 '09 at 01:15
  • There are some js functions declared on the page, but nothing that runs when the page is loaded - is that what I'd be looking for? – MGOwen Sep 16 '09 at 01:40
  • This is likely due to some kind of ajax control on your page interfering with the post. Start removing controls until you start executing events. –  Feb 22 '10 at 05:45

9 Answers9

15

During debugging, check the CausesValidation property of your button. For some reason, one of my pages had a button defaulting to "True." When I explicitly set it to "False" everything worked.

Charlie A
  • 151
  • 1
  • 3
9

I know that this should be resolved by now, but just to share - I just had a similar issue. In my case, the problem was that I had a RequiredFieldValidator in a page, that wasn't visible because it was part of a popup. That validator was supposed to be invoked only when clicking its related "Save" button, but since I didn't have any ValidationGroup set, it would prevent the page to submit for any clicked button. Adding a ValidationGroup to the right button resolved the issue.

Felipe Castro
  • 1,623
  • 11
  • 10
3

I had this problem just now and it was caused by another control having an AutoPostBack="true" which would submit the form before the button had a chance to submit the form to trigger the click event.

tomsv
  • 7,207
  • 6
  • 55
  • 88
3

Experienced the same issue recently - cause was eventually determined to be that the button was disabled by a juavascript method onbeforesubmit (designed to prevent multiple submissions), and unfortunately web forms appears to check the enabled state of the button before it allows the codebehind event to trigger.

annakata
  • 74,572
  • 17
  • 113
  • 180
2

Change

CodeFile="CompanyCompliance.aspx.cs" 

to

CodeBehind="CompanyCompliance.aspx.cs"

Related question: CodeFile vs CodeBehind

Community
  • 1
  • 1
TJB
  • 13,367
  • 4
  • 34
  • 46
  • Thanks, but it doesn't compile, I get "Could not load type 'CompanyCompliancePage'. CompanyCompliance.aspx 1". Could this be because it's in vs 2008 (asp.net 3.5) and so using partial classes for the code behind page? – MGOwen Sep 16 '09 at 01:32
  • Is there a .designer.cs class? Also make sure that Inherits="CompanyCompliancePage" matches your classname, you may need to fully qualify it... If all else fails, rename your current page, then do Add > New Item > Web Content Form > Name it 'CompanyCompliance' make sure you can just drop in a button and invoke the onclick handler, once that works then copy over as much as the markup / code as you can from the existing one. – TJB Sep 16 '09 at 01:38
  • I thought designer.cs and CodeBehind="" were mainly just for older versions of .net... that's what your link seems to be saying. Is that not correct? – MGOwen Sep 16 '09 at 02:15
  • Are you using ASP.net web forms (traditional) or ASP.net mvc? the latest version of Web Froms still uses code behind last I checked... I just came across that link and thought it might be useful, but I think its more complicated than what you're saying – TJB Sep 16 '09 at 07:46
  • No, web forms has used codefile instead of codebehind since 2005. Thanks anyway. – MGOwen Sep 23 '09 at 01:41
2

The inherits directive seems to point to a non-existant type.

Take a look in the code behind file of the CompanyCompliance page - ("CompanyCompliance.aspx.cs").

you should find the correct namespace and something like "public partial class xxx" where xxx is the name of the class which should correspond with your inherits directive. maybe you have to fully qualify the name as TJB already stated.

360Airwalk
  • 2,189
  • 1
  • 16
  • 13
2

I had a similar issue, and it was really tricky. I recently migrated a VS 2005 ASP.NET 2.0 web forms app to VS 2010 ASP.NET 4.0.

It was difficult to setup and buggy as expected, but the issue of buttons not firing the click event was getting on my nerves.

In the end, it was as simple as not declaring a submit action in the site.master form. I changed:

<FORM runat="server" name="MainForm" method="post" action="Default.aspx" id="m_mainForm" enctype="multipart/form-data">

to

<FORM runat="server" name="MainForm" method="post" id="m_mainForm" enctype="multipart/form-data">

Boom. Problem solved. I hope it saves days of troubleshooting to someone. Sadly, I couldn't find anything about it on the internet by myself.

Francisco
  • 4,104
  • 3
  • 24
  • 27
1

I had this problem I have changed the Button property "UseSubmitBehavior"=false. Now it is working.

K S BABJI
  • 11
  • 2
  • 1
    In the future, please be more detailed in your answers. If you provide an explanation of what you did and why you did it, preferably with code and/or official documentation, it'll be a great help to everyone. – DennisW Feb 20 '15 at 08:32
0

For future answer seekers : If you are assigning the click handler in code behind, make sure that it is not inside an IsPostBack == false check :

void Page_Load (object oSender, EventArgs oEventArgs)
{
   if (IsPostBack == false)
   {
       oButton += new EventHandler(oButton_Click); // does not work
   }
   oButton += new EventHandler(oButton_Click); // does work
}
Brett Weber
  • 1,839
  • 18
  • 22