ASP.NET button not firing on click event

2020-02-06 06:45发布

问题:

I am new to Asp.net and my question may not be so professional. I am using the design of Udemy.com in my asp.net web forms project. I put the header and footer of udemy in a masterpage. But when I add a button to one of m web forms the click event will not fire.

<asp:Button ID="Button1" runat="server" onclick="Button1_Click1" Text="Button" />

protected void Button1_Click1(object sender, EventArgs e)
{
    Page.Title = "Sample Text";
}

To fix this problem, I deleted all of the scripts from my pages. Also I tried to set CausesValidation to False. But nothing helps. Could you help me fix this problem?

Update

I added the OnClick event to my code. But I still face that problem.

回答1:

All the previous answers are correct, except that you "already added" the OnClick event to the ".aspx" file. I don't know whether it is something in the template you're using, or somewhere else in the code. But I would recommend that you check the following (Based on my experience in being stuck in similar situations):

  1. Do you have any Field Validators in your ".aspx" page? Perhaps one of those validators are fired and if they were without any Error Messages.
  2. Have you copied that button from somewhere else? If yes, then try to add a new button and add its click event, by double clicking the button in the design mode. As sometimes the "OnClick" button's event is not registered on the ".designer.cs" file.


回答2:

Some points you may try... The code that you posted (after update) should work.

  • If you open the Design part of your visual studio and double click in the button, This will lead you to the event click of that button, if there is not one event, then it'll create one for you.
  • Your button must be within the form tag.
  • To test the event click is firing, use Breakpoints not label or any other markup change, but if you want it ... you should check if the Title component has runat="server" attribute and it's within an update panel.


回答3:

This is a sample code , You have missed "OnClick="MyButton_Click" in your button.

aspx

<asp:Button ID="MyButton" **OnClick="MyButton_Click"** runat="server" />
code behind

protected void **MyButton_Click**(object sender, EventArgs e)
{
    // Put code here over there chuhu
}


回答4:

Add CausesValidation = "false" attribute of button like:

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" CausesValidation="false"></asp:Button>


回答5:

You are missing OnClick="Button1_Click". You need to add event like this

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"></asp:Button>


回答6:

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
}


回答7:

Your button should be like this

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>


回答8:

Can you please try this:

<asp:Button ID="Button1" runat="server" Text="Button" onClick="Button1_Click" />

You are not mentioning the event handler for the button click event.

hope it helps.



回答9:

Try to place Button within form tag

as

<form id="#" runat="Server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"></asp:Button>
</form>


回答10:

This is a sample code , You have missed "OnClick="MyButton_Click" in your button.

aspx

<asp:Button ID="MyButton" **OnClick="MyButton_Click"** runat="server" />

code behind

protected void **MyButton_Click**(object sender, EventArgs e)
{
    // Put code here
}

My personal answer is, You need to learn more things in Google

Some best tutorials for asp.net

http://www.w3schools.com/aspnet/default.asp

http://www.asp.net/web-pages/tutorials

http://asp.net-tutorials.com/

http://quickstarts.asp.net/QuickStartv20/default.aspx and can learn lot of knowledge from MSDN website

You can understand more asp.net controls and etc....



回答11:

Try this

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"></asp:Button>

Is this your page directive?

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


回答12:

Please check button is define properly in yourpage.aspx.designer.cs file.

something like below code:

protected global::System.Web.UI.WebControls.Button Button1;


回答13:

Page directive like this

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

HTML

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

the following code should be inside the Default.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
    {
        Page.Title = "Sample Text";
    }

My guess of where might be the problem is there may be problem in the directive

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

or else you wouldn't have called the asp:Button tag inside the form tag

 <form id="form1" runat="server">
    <div>

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

    </div>
    </form>


回答14:

Please Test the simple example in online aspdotnetfiddle.

Created sample program with single button & label control, when click on button, firing click event of button, in that assigning label text value.



回答15:

In my case, it wasn't firing the event because I was appending the button to the body of the master page and the <form> tag was in the <body> tag

I've solved the issue by appending the button to the <form> tag

Old code

$('body').append($(".ButtonsDiv"));

New code

$('#form1').append($(".ButtonsDiv"));


回答16:

If you have two buttons and two validation summaries, you must add ValidationGroup = "Group2" for each validiation summary, textbox and button.

And for the other add: ValidationGroup="Group1", it should work then.



回答17:

If the problem is validation firing and your button shouldn't trigger validation, consider adding CausesValidation="false" to the attributes of your button.



回答18:

Javascript can fail silently on your page, causing processing to be abandoned which prevents the click event from firing. If I suspect this is happening, I paste this immediately under the script tag before debugging:

window.onerror = function (message, filename, linenumber) {
        alert("JS error: " + message + " on line " + linenumber + " for " + filename);
}

Remove for production code.



回答19:

Check and try remove the attribute "required" on other input element. it works with in my case.. it took me while to find it out.

Read here this question also..same issue asp.net Button OnClick event not firing