I know you think there are many forums and posts about this old issue but I believe this is something else, becauase :
this issue is not browser related
the button event is fired, the postback occurs
this is not the aspnet_regiis... issue
I have one form on the masterpage, there is no other form
So what is my problem ?
First of all, if I create a new asp.net webapp, everything is working fine. I have created an applicataion for an online store which implements the business logic but contains no design elements.
After that I used a web desig application (ArtiSteer) from which I can export in asp.net format and create a new application. I can compile and run this webapp too. However, when I put a new asp button to the default.aspx page / lets say this, but any
page / the associated event handler is not working. Not under IE, not under Firefox. By debugging the application I found that the postback occurs to the server, the Page_Load event is fired and the Page.IsPostback is set to true, but the button's event handler never
gets called.
Unfortunately I've faced with this issue many times since I started to work with asp.net. Whenever I tried to copy an existing application's page to a new application, after debugging many hours I always had to redesign the original page in the new application
because this problem occured.
Now I feel really puzzled because I do not have any workaround and already spent days to resolve this issue
Check all of these things which are common to having a control's event not fire:
Make sure the 'Handles' statment is wired up to the control. Just because there is an event call MyButton_Click, it will not fire unless wired up. Sometimes when copying files the Handles statement might get truncated. It shoul look similar to the code
below:
Protected Sub btnContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinue.Click
Make sure you do not have both a 'Handles' statement wiring up the control and an 'onClick' event defined.
make sure that you do NOT have the following if the above 'Handles' statement is defined:
//Shouldn't have this and Handles statement
OnClick="MyButtonClickEvent"
Make sure 'AutoPostBack=True' is set on the control and did not accidentally get removed.
when I put a new asp button to the default.aspx page / lets say this, but any page / the associated event handler is not working. Not under IE, not under Firefox. By debugging the application I found that the postback occurs to the server, the Page_Load event
is fired and the Page.IsPostback is set to true, but the button's event handler never gets called.
Yes you are right in C# there is no handles statement. So you can either wire up the event handler in code manually (usually not needed for a button), or use the below code inside your button HTML code:
You are correct about the AutoPostBack property too, it is True by default.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = DateTime.Now.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
Page.Title = DateTime.Now.ToString();
}
}
This is a dummy code , but good enough to see that the postback occures but the Button1_Click event is not processed afterwards.
Lacko
0 Points
14 Posts
asp:Button onclick event is not working
Aug 10, 2009 04:28 PM|LINK
I know you think there are many forums and posts about this old issue but I believe this is something else, becauase :
So what is my problem ?
First of all, if I create a new asp.net webapp, everything is working fine. I have created an applicataion for an online store which implements the business logic but contains no design elements.
After that I used a web desig application (ArtiSteer) from which I can export in asp.net format and create a new application. I can compile and run this webapp too. However, when I put a new asp button to the default.aspx page / lets say this, but any page / the associated event handler is not working. Not under IE, not under Firefox. By debugging the application I found that the postback occurs to the server, the Page_Load event is fired and the Page.IsPostback is set to true, but the button's event handler never gets called.
Unfortunately I've faced with this issue many times since I started to work with asp.net. Whenever I tried to copy an existing application's page to a new application, after debugging many hours I always had to redesign the original page in the new application because this problem occured.
Now I feel really puzzled because I do not have any workaround and already spent days to resolve this issue
Have any of you ever met this problem ?
Many thanks,
Lacko
atconway
All-Star
16846 Points
2756 Posts
Re: asp:Button onclick event is not working
Aug 10, 2009 05:25 PM|LINK
Check all of these things which are common to having a control's event not fire:
Hope this helps!
Lacko
0 Points
14 Posts
Re: asp:Button onclick event is not working
Aug 10, 2009 05:58 PM|LINK
Thanks for your answer atconway, unfortunately I forgot to write that I am developing in c# not in vb.
As far as I know in c# there is no equivalent to the VB's Handles statement, but rather one have to explicitly define the onclick handler
and the AutoEventWireup directive should be set to true. These two conditions should be enough for the event to be dispatched to the
appropriate event handler in the codebehind code file. Correct me if I am mistaken , I am a beginner.
Also, there is no AutoPostBack property exposed for the Button class, I believe this is set to true by default.
Thx,
Lacko
budugu
All-Star
41188 Points
6034 Posts
Re: asp:Button onclick event is not working
Aug 10, 2009 06:01 PM|LINK
Please show us the page code..
"Don't be afraid to be wrong; otherwise you'll never be right."
atconway
All-Star
16846 Points
2756 Posts
Re: asp:Button onclick event is not working
Aug 10, 2009 06:06 PM|LINK
Yes you are right in C# there is no handles statement. So you can either wire up the event handler in code manually (usually not needed for a button), or use the below code inside your button HTML code:
Lacko
0 Points
14 Posts
Re: asp:Button onclick event is not working
Aug 10, 2009 06:27 PM|LINK
thx, here they are : (I do not think to post masterpage.master, it contains the html form and the content placeholders)
<%@ Page Language="C#" MasterPageFile="MasterPage.master" ValidateRequest="false" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %> <%@ Import Namespace="Artisteer" %> <%@ Register TagPrefix="artisteer" Namespace="Artisteer" %> <%@ Register TagPrefix="art" TagName="DefaultMenu" Src="DefaultMenu.ascx" %> <%@ Register TagPrefix="art" TagName="DefaultHeader" Src="DefaultHeader.ascx" %> <%@ Register TagPrefix="art" TagName="DefaultSidebar1" Src="DefaultSidebar1.ascx" %> <asp:Content ID="PageTitle" ContentPlaceHolderID="TitleContentPlaceHolder" Runat="Server"> Page title here... </asp:Content> <asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderContentPlaceHolder" Runat="Server"> <art:DefaultHeader ID="DefaultHeader" runat="server" /> </asp:Content> <asp:Content ID="MenuContent" ContentPlaceHolderID="MenuContentPlaceHolder" Runat="Server"> <art:DefaultMenu ID="DefaultMenuContent" runat="server" /> </asp:Content> <asp:Content ID="SideBar1" ContentPlaceHolderID="Sidebar1ContentPlaceHolder" Runat="Server"> <art:DefaultSidebar1 ID="DefaultSidebar1Content" runat="server" /> </asp:Content> <asp:Content ID="SheetContent" ContentPlaceHolderID="SheetContentPlaceHolder" Runat="Server"> <artisteer:Article ID="Article1" Caption="Welcome" runat="server"><ContentTemplate> <img class="art-article" src="images/spectacles.gif" alt="an image" style="float: left" /> <h1>Heading 1</h1> <h2>Heading 2</h2> <h3>Heading 3</h3> <h4>Heading 4</h4> <h5>Heading 5</h5> <h6>Heading 6</h6> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <p>Lorem ipsum dolor sit amet, <a href="javascript:void(0)" title="link">link</a>, <a class="visited" href="javascript:void(0)" title="visited link">visited link</a>, <a class="hover" href="javascript:void(0)" title="hovered link">hovered link</a> consectetuer adipiscing elit. Quisque sed felis. Aliquam sit amet felis. Mauris semper, velit semper laoreet dictum, quam diam dictum urna, nec placerat elit nisl in quam. Etiam augue pede, molestie eget, rhoncus at, convallis ut, eros.</p> <p> <span class="art-button-wrapper"> <span class="l"> </span> <span class="r"> </span> <a class="art-button" href="javascript:void(0)">Read more...</a> </span> </p> <table class="table" width="100%"> <tr> <td width="33%" valign="top"> <div class="art-Block"> <div class="art-Block-body"> <div class="art-BlockHeader"> <div class="l"></div> <div class="r"></div> <div class="t"><center>Support</center></div> </div> <div class="art-BlockContent"> <div class="art-PostContent"> <img src="images/01.png" width="55px" height="55px" alt="an image" style="margin: 0 auto; display: block; border: 0" /> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque sed felis. Aliquam sit amet felis. Mauris semper, velit semper laoreet dictum, quam diam dictum urna. </p> </div> </div> </div> </div> </td> <td width="33%" valign="top"> <div class="art-Block"> <div class="art-Block-body"> <div class="art-BlockHeader"> <div class="l"></div> <div class="r"></div> <div class="t"><center>Development</center></div> </div> <div class="art-BlockContent"> <div class="art-PostContent"> <img src="images/02.png" width="55px" height="55px" alt="an image" style="margin: 0 auto; display: block; border: 0" /> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque sed felis. Aliquam sit amet felis. Mauris semper, velit semper laoreet dictum, quam diam dictum urna. </p> </div> </div> </div> </div> </td> <td width="33%" valign="top"> <div class="art-Block"> <div class="art-Block-body"> <div class="art-BlockHeader"> <div class="l"></div> <div class="r"></div> <div class="t"><center>Strategy</center></div> </div> <div class="art-BlockContent"> <div class="art-PostContent"> <img src="images/03.png" width="55px" height="55px" alt="an image" style="margin: 0 auto; display: block; border: 0" /> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque sed felis. Aliquam sit amet felis. Mauris semper, velit semper laoreet dictum, quam diam dictum urna. </p> </div> </div> </div> </div> </td> </tr> </table> </ContentTemplate></artisteer:Article> <artisteer:Article ID="Article2" Caption="Who uses our products?" runat="server"><ContentTemplate> <p>Lorem <sup>superscript</sup> dolor <sub>subscript</sub> amet, consectetuer adipiscing elit, <a href="#" title="test link">test link</a>. Nullam dignissim convallis est. Quisque aliquam. <cite>cite</cite>. Nunc iaculis suscipit dui. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus. Maecenas ornare tortor. Donec sed tellus eget sapien fringilla nonummy. <acronym title="National Basketball Association">NBA</acronym> Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus. <abbr title="Avenue">AVE</abbr></p> <blockquote> <p> “This stylesheet is going to help so freaking much.” <br /> -Blockquote </p> </blockquote> <br /> <table class="art-article" border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <th>Header</th> <th>Header</th> <th>Header</th> </tr> <tr> <td>Data</td> <td>Data</td> <td>Data</td> </tr> <tr class="even"> <td>Data</td> <td>Data</td> <td>Data</td> </tr> <tr> <td>Data</td> <td>Data</td> <td>Data</td> </tr> </tbody></table> <p> <span class="art-button-wrapper"> <span class="l"> </span> <span class="r"> </span> <a class="art-button" href="javascript:void(0)">Join Now!</a> </span> </p> </ContentTemplate></artisteer:Article> </asp:Content>and the code file :
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Page.Title = DateTime.Now.ToString(); } protected void Button1_Click(object sender, EventArgs e) { Page.Title = DateTime.Now.ToString(); } }This is a dummy code , but good enough to see that the postback occures but the Button1_Click event is not processed afterwards.
budugu
All-Star
41188 Points
6034 Posts
Re: asp:Button onclick event is not working
Aug 10, 2009 06:36 PM|LINK
You have same code as in Page_Load. ...? Show us the page html also..
"Don't be afraid to be wrong; otherwise you'll never be right."
luappy13
Participant
1874 Points
408 Posts
Re: asp:Button onclick event is not working
Aug 10, 2009 06:56 PM|LINK
Obvious but annoying error in your code:
onclick="Button1_Click"
should be
OnClick="Button1_Click"
the onclick event is a client side event handler whereas OnClick is the asp.net server side handler
onclick not the same as OnClick
budugu
All-Star
41188 Points
6034 Posts
Re: asp:Button onclick event is not working
Aug 10, 2009 07:01 PM|LINK
Yes, it should be...
"Don't be afraid to be wrong; otherwise you'll never be right."
luappy13
Participant
1874 Points
408 Posts
Re: asp:Button onclick event is not working
Aug 10, 2009 08:13 PM|LINK
Yes, I thought I was right - thanks for confirming that it is the case
"
Obvious but annoying error in your code:
onclick="Button1_Click"
should be
OnClick="Button1_Click"
the onclick event is a client side event handler whereas OnClick is the asp.net server side handler"
Please mark this answer as correct so we dont keep re-visiting